How to add `unique` constraint to already existing index by migration

后端 未结 2 1967
南方客
南方客 2021-02-01 12:23

How can I add unique: true constraint to already existing index in Rails database?

I tried to migrate by

  def change
    add_index :editabi         


        
2条回答
  •  有刺的猬
    2021-02-01 12:48

    Remove the old index and add it again with the new constraint:

    def change
      remove_index :editabilities, [:user_id, :list_id]
      add_index :editabilities, [:user_id, :list_id], unique: true
    end
    

提交回复
热议问题