I have the following migration:
create_table :product_attr_vals do |t|
t.references :product, foreign_key: true
t.references :attr_val, foreign_key: true
t.timestamps
end
How can I add a unique index for the both t.references
?
Try to the following:
create_table :product_attr_vals do |t|
t.references :product, foreign_key: true
t.references :attr_val, foreign_key: true
t.timestamps
end
# Add this line
add_index :product_attr_vals, [:product_id, : attr_val_id], unique: true
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.