postgre数据库记录唯一键_本地postgres数据库不断给出错误重复键值违反唯一约束...

I don't understand why postgres is raising:

duplicate key value violates unique constraint

I went to check the table in pgadmin to see if the table really did have a duplicate and see:

Running VACUUM recommended

The estimated rowcount on the table deviates significantly from the actual rowcount.

Why is this happening? Luckily it doesn't seem to happen in production on heroku. It's a rails app.

Update:

Here is the sql log:

SQL (2.6ms) INSERT INTO "favorites" ("artist_id", "author_id",

"created_at", "post_id", "updated_at") VALUES ($1, $2, $3, $4, $5)

RETURNING "id" [["artist_id", 17], ["author_id", nil], ["created_at",

Sun, 18 Mar 2012 03:48:37 UTC +00:00], ["post_id", 62], ["updated_at",

Sun, 18 Mar 2012 03:48:37 UTC +00:00]] PG::Error: ERROR: duplicate

key value violates unique constraint

"index_favorites_on_artist_id_and_post_id" DETAIL: Key (artist_id,

post_id)=(17, 62) already exists.

But in the actual table there is no such record with artist_id = 17 and post_id = 62. But postgres believes that there is.

解决方案

actually i think the problem is not related to postgres at all. It was a combination of simultaneous duplicate ajax calls creating what would be a duplicate record, then neither are inserted.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值