Ruby on rails 为什么PG::UniqueViolation:错误:重复键值违反唯一约束?
我有一个模型Ruby on rails 为什么PG::UniqueViolation:错误:重复键值违反唯一约束?,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我有一个模型Post,每次创建一个Post时,我都希望同时创建一个Moderation的新实例 因此,在post.rb中,我在保存之后使用回调:create\u moderation 然后编写一个私有方法: ... include Reportable after_save :create_moderation private def create_moderation self.create_moderation!(blog: Blog.first) end 但当创建提
Post
,每次创建一个Post时,我都希望同时创建一个Moderation
的新实例
因此,在post.rb中,我在保存之后使用回调:create\u moderation
然后编写一个私有方法:
...
include Reportable
after_save :create_moderation
private
def create_moderation
self.create_moderation!(blog: Blog.first)
end
但当创建提案时,我会遇到以下错误:
PG::UniqueViolation:错误:重复的键值违反了唯一约束“可报告”详细信息:键(可报告类型,可报告id)=(Post,25)已存在:将($1、$2、$3、$4、$5、$6)返回“id”的值插入“审核”(“博客id”、“可报告类型”、“可报告id”、“创建时间”、“更新时间”、“博客类型”)
在reportable.rb中,我有:
has_one :moderation, as: :reportable, foreign_key: "reportable_id", foreign_type: "reportable_type", class_name: "Moderation"
然后对可报告对象使用其他一些方法
注意,当我在控制台中运行create方法时,这个问题不会发生
编辑
create_table "moderations", id: :serial, force: :cascade do |t|
t.string "reportable_type", null: false
t.string "reportable_id", null: false
t.integer "blog_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "blog_type", null: false
t.string "upstream_moderation", default: "unmoderate"
t.index ["blog_id", "blog_type"], name: "moderations_blog"
t.index ["reportable_type", "reportable_id"], name: "moderations_reportable", unique: true
end
create_table "posts", id: :serial, force: :cascade do |t|
t.text "title", null: false
t.text "body", null: false
t.integer "feature_id", null: false
t.integer "author_id"
t.integer "scope_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "post_votes_count", default: 0, null: false
t.index ["body"], name: "post_body_search"
t.index ["created_at"], name: "index_posts_on_created_at"
t.index ["author_id"], name: "index_posts_on_author_id"
t.index ["feature_id"], name: "index_posts_on_feature_id"
t.index ["proposal_votes_count"], name: "index_posts_on_post_votes_count"
t.index ["title"], name: "post_title_search"
end
看起来您已将唯一索引添加到数据库:
t.index ["reportable_type", "reportable_id"], name: "moderations_reportable", unique: true
ActiveRecord::Base.connection.tables.each do |table_name|
ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
end
使用唯一索引,您将只能有一条记录具有相同的
reportable\u type
和reportable\u id
。很可能您正在尝试为已经有审核的可报告项创建审核。要解决此问题,我们必须告诉ActiveRecord查看表的顺序:
ActiveRecord::Base.connection.reset_pk_sequence!('table_name')
现在ActiveRecord应该具有正确的序列值,并且应该能够正确分配新id
解决错误
PG::UniqueViolation:错误:重复的键值违反了唯一约束“可报告”详细信息:键(可报告类型,可报告id)=(Post,25)已存在:将($1、$2、$3、$4、$5、$6)返回“id”的值插入“审核”(“博客id”、“可报告类型”、“可报告id”、“创建时间”、“更新时间”、“博客类型”)
“审核”表上发生错误
从rails控制台运行以下命令
ActiveRecord::Base.connection.reset_pk_sequence!('moderations')
感谢您修复所有数据库的pkey序列:
t.index ["reportable_type", "reportable_id"], name: "moderations_reportable", unique: true
ActiveRecord::Base.connection.tables.each do |table_name|
ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
end
请也向您展示迁移,因为这是一个数据库错误,而不是ORMError@engineersmnky我编辑了我的帖子,这个
t.index[“reportable\u type”,“reportable\u id”],名称:“moderations\u reportable”,unique:true
没有泄露?抱歉,复制粘贴错误没有错误,该行仍然是数据库键约束的一部分。我想你应该在创建后将钩子改为,而不是保存后的,因为save
也会在update
上被调用。你是我的朋友。这是永久性的解决方案吗?你认为这个问题会再次出现吗?是的,但是如果你再次弄乱它,你需要重新设置序列。你是对的,你有什么自动解决方法吗?这让我的隐藏完全不相关。明亮的