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

后端 未结 2 1968
南方客
南方客 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:28

    Faster way using add_index_options:

    def change
      add_index_options :editabilities, [:user_id, :list_id], unique: true
    end
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题