[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB

It is a memorandum.

What is a unique constraint?

It is a unique constraint. Constraints when adding or updating data in DB. Gives the column its own uniqueness.

For example, by applying a unique constraint, when a user makes a new registration, an error can be returned if he / she tries to register with the same email address that has already been registered.

How to write unique

add_index :table name, [:Column name 1, :Column name 2, :Column name 3, ...(Continue)], unique: true

If the table name is contents and the column names are part_id and word, it will be as follows.

add_index :contents, [:part_id, :word], unique: true

Description location

Let's describe it in the change method of the migration file as follows.

Migration file


class CreateContents < ActiveRecord::Migration[6.0]
  def change
    create_table :contents do |t|
      t.integer :part_id, null: false
      t.string  :word, null: false
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end
    add_index :contents, [:part_id, :word], unique: true #This line.
  end
end

If you make a mistake in the description location

For example, remove it from the change method and write it as follows.

Migration file


class CreateContents < ActiveRecord::Migration[6.0]
  def change
    create_table :contents do |t|
      t.integer :part_id, null: false
      t.string  :word, null: false
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end
  end
  add_index :contents, [:part_id, :word], unique: true #This line.
end

If you run rails db: migrate in this state, you will get an error.

% rails db:migrate
-- add_index(:contents, [:part_id, :word], {:unique=>true})
-- add_index(:contents, [:part_id, :word], {:unique=>true})
rails aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'hoge_development.contents' doesn't exist

Recommended Posts

[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB
[Ruby On Rails] How to reset DB in Heroku
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Basic knowledge of Ruby on Rails
How to use Ruby on Rails
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
Ruby on Rails DB Tips for creating methods to reduce the load
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] How to use CarrierWave
Let's summarize "MVC" of Ruby on Rails
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Ruby on Rails] Button to return to top
[Rails] Implementation of validation that maintains uniqueness
[Ruby on Rails] How to make the link destination part of the specified id
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Try to create a service that makes local cats happy
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Implementation of Ruby on Rails login function (Session)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[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] Individual display of error messages
[Ruby on Rails] Implementation of validation that works only when the conditions are met
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[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
Implementation of Ruby on Rails login function (devise edition)
Docker the development environment of Ruby on Rails project
[Ruby on Rails] How to change the column name
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
[Ruby on Rails] Implementation of tagging function/tag filtering function
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Ruby on Rails] From MySQL construction to database change
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) How to create models and tables