Create table Delete table Add column Column name change Column data type change Delete column
The migration file is created when you create the model. It is also possible to create the migration file alone.
Terminal
rails g model [Model name] [Attribute name:データ型 Attribute name:Data type ...] [option]
#When generating by itself
rails g migration migration name
By executing the above command, a migration file will be created in the db / migrate folder.
Official information | Rough overview in Japanese |
---|---|
primary_key | Primary key |
string | String(1-255 characters) |
text | Long string(1 to 4294967296 characters) |
integer | integer(4 bytes) |
bigint | integer(8 bytes) |
float | Floating point |
decimal | Highly accurate decimal |
numeric | Numerical value |
datetime | Date and time |
time | time |
date | date |
binary | Binary data |
boolean | Boolean type |
** Which is better, string or text, when dealing with characters **
Since string can handle only 255 characters, it is recommended to use text when handling data that may be 256 characters or more depending on the state and time. Character information such as names, email addresses, and company names are generally handled as strings, and text information such as text and remarks is generally used as text.
** Which is better, string or integer when dealing with enums ** Since it is enum that can map the integer of hash, if the column to be handled by enum is set to string, there is no point in incorporating enum in the first place, and it does not work. With that in mind, integer is a good choice for the column data types that enum will handle.
** Which is better, integer or bigint, when dealing with id ** The id can be a huge number of digits as the number of users increases in the future. Given that the id column defaults to bigint from Rails 5.1, you can basically think that bigint is suitable.
In either case, it is safer to select a larger box if there is any possibility that it will not fit in the future, rather than matching it with the current situation.
In Rails, the table name of the database corresponding to model is plural like members.
However, the model class name is singular with an uppercase letter, such as Member.
Also, when creating a model, it's okay to start the member in lowercase like "rails g model member".
Also, for example, a MemberImage model (table name is member_images) is created by specifying member_image or as MemberImage.
However, it is strictly forbidden to pluralize it like members. The Members model will be created. Be sure to create the model in the singular.
Recommended Posts