So I am trying to add an index on my votes table to prevent duplicate rows from being added.
In my previous stackoverflow question. I asked "How do I ensure that duplicate rows are not added to my database table via activerecords?" How do I ensure that duplicate rows are not added to my database table via activerecords?
The answer was adding an index through a database migration and this solved it.
class AddUniqueIndexToVotes < ActiveRecord::Migration
def change
add_index :votes, [:voter_id, :votefor_id, :vote], unique: true
end
end
However, now I get the error.
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.
How do I automatically delete the duplicate rows/row when the migration is run.
thank you!