[Ruby on Rails] Column restrictions when saving to DB (4 representatives)

Background of writing this article

I learned that there is a way to specify constraints when saving data for migration files and model files, so I tried to summarize typical validations again. (I studied a long time ago, but I will summarize it in qiita again.)

Implementation environment

macOS Catalina 10.15.7 VS Code 1.50.0 Ruby 2.6.5 Rails 6.0.0

① NOT NULL constraint

→ Restriction that cannot be saved when the data is empty

[How to write in the migration file] null: false

20200919092740_create_item.rb


t.string :address, null: false

[How to write in the model file] presence: true

item.rb


validates :address, presence: true

② Uniqueness constraint

→ Restriction that the same data cannot be saved more than once [How to write in the migration file] unique: true

20200919092740_create_item.rb


t.string :address, unique: true

[How to write in the model file] uniqueness: true

item.rb


validates :address, uniqueness: true

③ Primary key constraint

→ Restriction that data cannot be saved when it is empty and the same data cannot be saved more than once. (NOT NULL constraint and unique constraint) (Restrictions set automatically for id column)

[How to write in the migration file] primary_key: true

20200919092740_create_item.rb


t.string :address, primary_key: true

④ Foreign key constraint

→ Restriction that data cannot be saved unless the data that becomes the foreign key always exists

[How to write in the migration file] foreign_key: true

20200919092740_create_item.rb


t.string :address, foreign_key: true

Recommended Posts

[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
What to do when Blocked Host: "host name" appears in Ruby on Rails
Ruby on Rails DB Tips for creating methods to reduce the load
Ruby on Rails When you don't know the cause of rollback when saving.
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
When the Ruby on Rails terminal rolls back
[Rails MySQL] How to reset DB on heroku
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Updated from time to time] Ruby on Rails Convenient methods
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Ruby on Rails Tutorial Troublesome notes when running on Windows
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
BasicDataSourceFactory is ClassNotFoundException when connecting to DB on Tomcat 8
Settings to be done when changing from Sublime Text to VScode (when writing Ruby On Rails)
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
Ruby on rails learning record -2020.10.03
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
Portfolio creation Ruby on Rails
[Ruby on Rails] When parameter id acquisition does not work
Ruby on rails learning record -2020.10.04
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
[Ruby on Rails] Add a column with a foreign key constraint
Ruby on Rails basic learning ①
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
[Ruby on Rails] about has_secure_password
What to check when rails db: migration does not pass
Ruby on rails learning record-2020.10.07 ②
From Ruby on Rails error message display to Japanese localization
How to specify db when creating an app with rails
How to display a graph in Ruby on Rails (LazyHighChart)
Commentary on partial! --Ruby on Rails
[Rails] Add column to devise
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Apply CSS to a specific View in Ruby on Rails
Ruby on Rails validation summary