Duplicate entries created by find_or_create_by_

I have an ActiveRecord object, a corporation, and the only call in my project to instantiate this object is as follows:

corp = Corporation.find_or_create_by_eveid_and_user_id(self.corporation_eveid, self.account.user_id)

However, after my application runs happily for several days, there are duplicate entries - a record where eveid and user_id have the same value. How is this possible? Is there something that I could do wrong in the way I update these records that might cause this problem?

In the end, I added a unique unique composite index to the table. This should solve the problem, but I do not understand how this happens.

This is Rails 3.0.7.

+3
source share
2

find_or_create . . , :

  • , , -
  • (, ), . , , . MySQL InnoDB SELECT ... FOR UPDATE. (.. -, ), (1).

| UNIQUE , , .

+5

? , eveid user_id.

, , find_or_create, . , , ​​ - , . .

, - .

validates :eveid, :uniqueness => { :scope => :user_id }
0

All Articles