Define a unique primary key based on 2 columns

后端 未结 7 722
长情又很酷
长情又很酷 2021-01-31 02:52

I would like to define a unique key for records based on 2 columns : \'id\' and \'language\'

to let the user submits the following strings : id=1 language=en value=bl

7条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 03:15

    In Rails 5 you can do the following:

    create_table :words, primary_key: %i[id language_id] do |t|
      t.integer :id
      t.integer :language_id
      t.string :value
      t.timestamps
    end
    

    It is also NOT necessary to set the primary_key attribute on the Word model.

提交回复
热议问题