Specify Custom Index Name When Using Add_Reference

Specify custom index name when using add_reference

As I commented, do :

add_index :table, :column, name: 'index name' 

Here is documentation.
Or, you can try this :

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
def change
add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index name' }
end
end

migrations: t.references doesn't allow index name to be specified

According to Rails code for references, you can do so, providing index a Hash with options, the one you need called :name, so:

t.references :my_field, index: { name: 'my_index_name' }

How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

Provide the :name option to add_index, e.g.:

add_index :studies,
["user_id", "university_id", "subject_name_id", "subject_type_id"],
unique: true,
name: 'my_index'

If using the :index option on references in a create_table block, it takes the same options hash as add_index as its value:

t.references :long_name, index: { name: :my_index }

Specifying column name in a references migration

Do it manually:

add_column :post, :author_id, :integer

but now, when you create the belongs_to statement, you will have to modify it, so now you have to call

def post
belongs_to :user, :foreign_key => 'author_id'
end

Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention

EDIT: For those that see the tick and don't continue reading!

While this answer achieves the goal of having an unconventional foreign key column name, with indexing, it does not add a fk constraint to the database. See the other answers for more appropriate solutions using add_foreign_key and/or 'add_reference'.

Note: ALWAYS look at the other answers, the accepted one is not always the best!

Original answer:

In your AddReferencesToPeople migration you can manually add the field and index using:

add_column :people, :foo_bar_store_id, :integer
add_index :people, :foo_bar_store_id

And then let your model know the foreign key like so:

class Person < ActiveRecord::Base
has_one :store, foreign_key: 'foo_bar_store_id'
end

Ruby on rails migration with foriegn key and index

The reason you're getting two database indices is that the index option for add_reference (this is the method wrapped by references) defaults to true.

If needed you can configure the index by passing the same options that add_index takes:

t.references :task, null: false, 
foreign_key: { on_delete: :cascade },
index: { name: 'index_task_id' }

And you can also suppress the generation of the index by passing false.

I think that is old way when references in migration were not used.

The default was changed in Rails 5.

Index name index_name is too long; the limit is 63 characters

You can try creating the index with a custom (and shorter) name:

add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'my_custom_and_shorter_name')

Since you already have the name field there, just change the index name :)

Add a reference column migration in Rails 4

Rails 4.x

When you already have users and uploads tables and wish to add a new relationship between them.

All you need to do is: just generate a migration using the following command:

rails g migration AddUserToUploads user:references

Which will create a migration file as:

class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
end
end

Then, run the migration using rake db:migrate.
This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.

UPDATE [For Rails 4.2]

Rails can’t be trusted to maintain referential integrity; relational databases come to our rescue here. What that means is that we can add foreign key constraints at the database level itself and ensure that database would reject any operation that violates this set referential integrity. As @infoget commented, Rails 4.2 ships with native support for foreign keys(referential integrity). It's not required but you might want to add foreign key(as it's very useful) to the reference that we created above.

To add foreign key to an existing reference, create a new migration to add a foreign key:

class AddForeignKeyToUploads < ActiveRecord::Migration
def change
add_foreign_key :uploads, :users
end
end

To create a completely brand new reference with a foreign key(in Rails 4.2), generate a migration using the following command:

rails g migration AddUserToUploads user:references

which will create a migration file as:

class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
add_foreign_key :uploads, :users
end
end

This will add a new foreign key to the user_id column of the uploads table. The key references the id column in users table.

NOTE: This is in addition to adding a reference so you still need to create a reference first then foreign key (you can choose to create a foreign key in the same migration or a separate migration file). Active Record only supports single column foreign keys and currently only mysql, mysql2 and PostgreSQL adapters are supported. Don't try this with other adapters like sqlite3, etc. Refer to Rails Guides: Foreign Keys for your reference.



Related Topics



Leave a reply



Submit