Mysql ::错误:重复条目
我有一个模特
class Gift :account_id end add_index(:gifts, [:account_id, :giver_id], :uniq => true)
行动
def create @gift= Gift.new(params[:gift]) if @gift.save ... else ... end end
在“生产”模式中,我有时会收到错误
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '122394471958-50301499' for key 'index_gifts_on_account_id_and_user_id'
有什么问题?
看起来gifts
表具有account_id
和user_id
的唯一索引。
如果需要此索引,请为模型添加唯一性检查:
class Gift < ActiveRecord::Base validates_uniqueness_of :giver_id, :scope => :account_id validates_uniqueness_of :user_id, :scope => :account_id end
否则删除索引。
DROP INDEX index_gifts_on_account_id_and_user_id ON gifts
编辑:尝试为giver_id
添加状态检查。
class Gift < ActiveRecord::Base validates_presence_of :giver_id validates_uniqueness_of :user_id, :scope => :account_id end
- 在heroku上插入数据库错误但在本地工作,ActiveRecord :: StatementInvalid
- OmniAuth Railscast中的DangerousAttributeError教程:create由ActiveRecord定义
- t。模型中迁移vs belongs_to的参考?
- 您将如何使用rails和ActiveRecord中的引用和引用对文章进行建模?
- 在查询随机简单查询时丢失与MySQL服务器的连接
- ConnectionNotEstablished之后在JRuby中有一个multithreading环境
- 使用ActiveRecord模型生成输入ID
- has_many:通过has_and_belongs_to_many关联
- Rails:渴望加载as_json包含