How to Create a New Table With a Unique Index in an Active Record / Rails 4 Migration

Ruby on-RailsActiverecordRuby on-Rails-4

Ruby on-Rails Problem Overview


How do I create a new table, through a rails migration, and add an unique index to it?

In the docs I found how to add a index to a table after it's been created, but how do you do both -- create the table, and add the unique index -- in the same migration file?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Here's the full process:

Generate a migration ( rails generate migration CreateFoos bar:string or rails g migration CreateFoos bar:string )

Modify your migration to look something like this:

class CreateFoos < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.string :bar, :null => false

      t.index :bar, unique: true
    end
  end
end

Run rake db:migrate

Solution 2 - Ruby on-Rails

A more compact way:

class CreateFoobars < ActiveRecord::Migration
  def change
    create_table :foobars do |t|
      t.string :name, index: {unique: true}
    end
  end
end

Solution 3 - Ruby on-Rails

After generating a migration rails generate migration CreateBoards name:string description:string

In the migration file, add index as shown below:

class CreateBoards < ActiveRecord::Migration
  def change
   create_table :boards do |t|
     t.string :name
     t.string :description
 
     t.timestamps
   end

   add_index :boards, :name, unique: true

 end
end

Solution 4 - Ruby on-Rails

You can create the table and index with the generator without changing the migration file

For a unique index

rails generate model CreateFoos bar:string:uniq

For a non-unique index

rails generate model CreateFoos bar:string:index

Solution 5 - Ruby on-Rails

In Rails 5, you can provide index options along with column definition.

create_table :table_name do |t|
  t.string :key, null: false, index: {unique: true}
  t.jsonb :value

  t.timestamps
end


Column   |            Type             | Collation | Nullable |                 Default
------------+-----------------------------+-----------+----------+-----------------------------------------
id         | bigint                      |           | not null | nextval('table_name_id_seq'::regclass)
key        | character varying           |           | not null |
value      | jsonb                       |           |          |
created_at | timestamp without time zone |           | not null |
updated_at | timestamp without time zone |           | not null |
Indexes:
  "table_name_pkey" PRIMARY KEY, btree (id)
  "index_table_name_on_key" UNIQUE, btree (key)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnewUserNameHereView Question on Stackoverflow
Solution 1 - Ruby on-RailsnewUserNameHereView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAkaZecikView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKirti ThoratView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRandomtheoriesView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAkanksha SharmaView Answer on Stackoverflow