I'm writing a migration that involves a foreign key. Looking at my colleagues code, I see that he has added the line: t.reference :tablename, index: true
The t.reference part makes sense, but I don't know what index: true
means. Can anyone tell me? I haven't been able to find that in the docs.
Note: This is not a duplicate of: Rails ActiveRecord::Migration what is the difference between index: true and add_index? Which only diffs the two, but doesn't explain what they do.
index: true
adds a database index to the referenced column. For example, if creating a :products table:
create_table :products do |t|
t.references :user, index: true
end
That will create a non-unique index on the user_id
column in the products
table named index_products_on_user_id
.
well, when you create this add_reference thing you are saying to rails to add a user_id within "product table". i think the id's are useful to connect differents tables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.