Reputation:
I have a User
table and a Product
table. There is a has_many :products
and a belongs_to :user
relationship. I defined the Product and User models correctly...my question is about setting up the migration so that the schema comes out right. I tried and this was the extra line in the schema that i got.
add_index "product", ["user_id"], name: "index_product_on_user_id"
I then had a look online and instead of add_index other code had add_reference...does that matter? And not sure about the name: "index_product_on_user_id". Thanks!
Upvotes: 1
Views: 32
Reputation: 84114
add_reference
adds the required columns and (optionally) creates an index whereas add_index
just creates the index, so if you want to use add_index
you need to create the column separately.
You almost never need to set the index name - it doesn't affect how the index works or is used (one case is where the autogenerated index name exceeds the maximum index length).
Upvotes: 2