简体   繁体   中英

How to add a unique index between two references columns in Rails Migration

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM