我是一名在rails api上工作的新手ruby程序员。问题是api正在生产中,现在我想为模型中的某个列添加唯一约束。目前重复的条目是允许的,我想让这个列是唯一的。通过迁移不起作用添加唯一约束
所以我加了两个固定装置具有相同的名称是这样的:
two:
name: MyString2
location:
status: 2
three:
name: MyString2
location:
status: 1
然后我试图运行这样的迁移:
class AddUniqueToLocationColumnName < ActiveRecord::Migration
class User < ActiveRecord::Base
end
def self.up
remove_index :locations, column: :name
add_index :locations, :name, unique: true
end
def self.down
remove_index :locations, column: :name # remove unique index
add_index :locations, :name # adds just index, without unique
end
end
但其示值误差:“重复存在于数据库。迁移失败。“ 同样是问题。我已经在生产表中有重复。我想为表“位置”中的列“名称”添加一个唯一的约束。我如何使这个专栏独一无二?
它是b'coz重复的记录存在于数据库,首先,从DB中删除重复记录,然后尝试运行。 – Amit
我想通过迁移将其删除。通过一个红宝石代码找到所有重复项并销毁它们 – Abhishek