为什么 PG::UniqueViolation:错误:重复键值违反唯一约束? [英] why PG::UniqueViolation: ERROR: duplicate key value violates unique constraint?

查看:240
本文介绍了为什么 PG::UniqueViolation:错误:重复键值违反唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型 Post,每次创建帖子时,我都希望同时创建一个新的 Moderation 实例.

所以在 post.rb 中我使用回调 after_save :create_moderation然后写一个私有方法:

<代码> ...包括可报告after_save :create_moderation私人的def create_moderationself.create_moderation!(博客:Blog.first)结尾

但是当创建提案时,我收到此错误:

<块引用><块引用>

PG::UniqueViolation:错误:重复键值违反唯一约束moderations_reportable"详细信息:键 (reportable_type, reportable_id)=(Post, 25) 已经存在.: INSERT INTO "moderations" ("blog_id", "reportable_type", "reportable_id", "created_at", "updated_at", "blog_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "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: falset.string "reportable_id", null: falset.integer "blog_id", null: falset.datetime "created_at", null: falset.datetime "updated_at", null: falset.string "blog_type", null: falset.string "upstream_moderation", 默认值: "unmoderate"t.index ["blog_id", "blog_type"], name: "moderations_blog"t.index ["reportable_type", "reportable_id"], 名称: "moderations_reportable", 唯一的: true结尾create_table "posts", id: :serial, force: :cascade do |t|t.text "title", null: falset.text "body", null: falset.integer "feature_id", null: falset.integer "author_id"t.integer "scope_id"t.datetime "created_at", null: falset.datetime "updated_at", null: falset.integer "post_votes_count",默认值:0,空值:falset.index ["body"],名称:"post_body_search"t.index ["created_at"],名称:"index_posts_on_created_at"t.index ["author_id"],名称:"index_posts_on_author_id"t.index ["feature_id"],名称:"index_posts_on_feature_id"t.index ["proposal_votes_count"],名称:"index_posts_on_post_votes_count"t.index ["title"],名称:"post_title_search"结尾

解决方案

看起来您已经向数据库添加了唯一索引:

t.index ["reportable_type", "reportable_id"], name: "moderations_reportable", unique: true

使用唯一索引,您将只能拥有一个具有相同 reportable_typereportable_id 的记录.您可能正在尝试为已进行审核的可举报内容创建审核.

I have a model Post and each time a post is created I want a new instance of Moderation to be created at the same time.

So in post.rb I use the callback after_save :create_moderation Then write a private method :

 ...
 include Reportable
 after_save :create_moderation

 private
 def create_moderation
    self.create_moderation!(blog: Blog.first)
 end

But when a proposal is created I get this error :

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "moderations_reportable" DETAIL: Key (reportable_type, reportable_id)=(Post, 25) already exists. : INSERT INTO "moderations" ("blog_id", "reportable_type", "reportable_id", "created_at", "updated_at", "blog_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"

In reportable.rb I have :

  has_one :moderation, as: :reportable, foreign_key: "reportable_id", foreign_type: "reportable_type", class_name: "Moderation"

Then few other methods for reportable object.

Note that this issue doesn't happen when I run the create method in console.

EDIT

  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

解决方案

Looks like you've added a unique index to your database:

t.index ["reportable_type", "reportable_id"], name: "moderations_reportable", unique: true

With a unique index you will only be able to have one record with the same reportable_type and reportable_id. It's likely that you're trying to create a moderation for a reportable that already has a moderation.

这篇关于为什么 PG::UniqueViolation:错误:重复键值违反唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆