MysqL2::Error: Duplicate entry '3022093-2000000028003-visited' for key 'unique_user_place_relationship'
虽然我已将问题的根源缩小到以下几行:
begin up = UserPlace.new(user_place_params) up.skip_logging up.save!rescue ActiveRecord::RecordNotUnique => e Rails.logger.warn(e)end
在我的表中,我有以下索引:
key_name seq_in_index column_nameunique_user_place_relationship 1 user_IDunique_user_place_relationship 2 place_IDunique_user_place_relationship 3 relationship
问题是我的user_place.rb中没有valIDate_uniqueness_of user_ID,place_ID和relationship吗?
根据我的理解,ActiveRecord:RecordNotUnique应该捕获此错误,因为事务不满足db级别的索引约束.
解决方法 doc说:总结Using this valIDation method in conjunction with
ActiveRecord::ValIDations#save
does not guarantee the absence of
duplicate record insertions,becauseuniqueness
checks on the application level are inherently prone to race conditions. For example,suppose that two users try to post a Comment at the same time,and a Comment’s Title must be unique. At the database-level,the actions performed by these users Could be interleaved in the following manner…
以上是内存溢出为你收集整理的ruby-on-rails – Mysql2 ::错误:密钥的重复条目 – ActiveRecord :: RecordNotUnique没有捕获错误?全部内容,希望文章能够帮你解决ruby-on-rails – Mysql2 ::错误:密钥的重复条目 – ActiveRecord :: RecordNotUnique没有捕获错误?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)