What is the right migration data type? ??

What can be achieved by migration

Create table Delete table Add column Column name change Column data type change Delete column

Creation of migration file

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.

What is the best data type? ??

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

What to do with similar but different things

** 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.

Supplement

Rails model naming convention

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

What is the right migration data type? ??
What is the data structure of ActionText?
The migration file is duplicated
What is a boolean type?
What is the pluck method?
What is the BufferedReader class?
What is the constructor for?
What is the initialize method?
'% 02d' What is the percentage of% 2?
What is a column Boolean type?
What is a reference type variable?
What is testing? ・ About the importance of testing
[Personal memo] Java data type is annoying
What is the best file reading (Java)
What is the model test code testing
What is the main method in Java?
What is the Facade pattern useful for?
What is JSP? ~ Let's know the basics of JSP !! ~
What is Cubby
What is null? ]
What is java
What is Keycloak
What is the Java Servlet / JSP MVC model?
What is maven?
What is Jackson?
What is different from the PHP language. [Note]
The intersection type introduced in Java 10 is amazing (?)
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is the volatile modifier for Java variables?
What is Gradle?
What is POJO
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is Java
What is centOS
Organized memo in the head (Java --Data type)
What is RubyGem?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
[Java] Data type ①-Basic type
What is hard coding?
What is a stream
What is Ruby's attr_accessor?