【问题标题】:How to add unique: true to already existing column如何添加唯一的:true 到已经存在的列
【发布时间】:2014-11-17 11:00:53
【问题描述】:

我通过迁移向表中添加了一个列。它生成了一个迁移文件,我添加了以下内容。然后迁移。添加后我意识到要添加忘记唯一:真正的迁移。如何将unique: true 添加到此迁移文件。请语法。

迁移中

def change
  add_column :tasks, :position, :integer
end

【问题讨论】:

标签: ruby-on-rails-4 migration


【解决方案1】:

删除旧索引并使用新约束再次添加:

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

(信用:我直接从 Baldrick 的回答 here 中提取了这个答案。在你给我投票之前先给他投票;))

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2011-03-04
    • 2013-06-11
    • 2016-04-10
    • 2018-02-21
    • 1970-01-01
    • 2021-06-09
    • 2020-02-19
    • 1970-01-01
    相关资源
    最近更新 更多