Mysql::Error: Duplicate entry
I have a model
class Gift < ActiveRecord::Base
validates_uniqueness_of :giver_id, :scope => :account_id
end
add_index(:gifts, [:account_id, :giver_id], :uniq => true)
Action
def create
@gift= Gift.new(params[:gift])
if @gift.save
...
else
...
end
end
In the "production" mode, I sometimes get an error
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '122394471958-50301499' for key 'index_gifts_on_开发者_高级运维account_id_and_giver_id'
What the problem?
Your is made of two values :account_id and :giver_id. If you say :unique => true on your index that means that you are expecting the combination of :account_id and :giver_id to yield a unique value which will be inserted into the index.
精彩评论