【发布时间】:2014-07-06 14:49:55
【问题描述】:
所以我试图在我的投票表上添加一个索引,以防止添加重复的行。
在我之前的 stackoverflow 问题中。我问“如何确保不会通过 activerecords 将重复的行添加到我的数据库表中?” How do I ensure that duplicate rows are not added to my database table via activerecords?
答案是通过数据库迁移添加索引,这解决了它。
class AddUniqueIndexToVotes < ActiveRecord::Migration
def change
add_index :votes, [:voter_id, :votefor_id, :vote], unique: true
end
end
但是,现在我得到了错误。
PG::Error: ERROR: could not create unique index "index_votes_on_voter_id_and_votefor_id_and_vote"
DETAIL: Key (voter_id, votefor_id, vote)=(581, 519, 2) is duplicated.
如何在迁移运行时自动删除重复的行/行。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby postgresql activerecord rails-migrations