本地postgres数据库不断给出错误重复键值违反唯一约束 [英] local postgres db keeps giving error duplicate key value violates unique constraint
问题描述
我不明白为什么postgres正在提高:
I don't understand why postgres is raising:
duplicate key value violates unique constraint
我去检查pgadmin中的表,看看表是否真的有重复,看看:
I went to check the table in pgadmin to see if the table really did have a duplicate and see:
运行VACUUM推荐
表上的估计行数与实际行数显着偏差。
The estimated rowcount on the table deviates significantly from the actual rowcount.
为什么会发生这种情况?幸运的是,在英雄的生产中似乎不会发生。这是一个rails应用程序。
Why is this happening? Luckily it doesn't seem to happen in production on heroku. It's a rails app.
更新:
这是sql日志:
SQL(2.6ms)INSERT INTOfavorites(artist_id,author_id,
created_at post_id,updated_at)VALUES($ 1,$ 2,$ 3,$ 4,$ 5)
RETURNINGid[[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 ::错误:错误:重复
键值违反唯一约束
index_favorites_on_artist_id_and_post_idDETAIL:键(artist_id,
post_id)=(17,62)已存在。 / p>
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.
但是在实际的表格中没有这样的记录,artist_id = 17,post_id = 62。但是postgres认为有。 p>
But in the actual table there is no such record with artist_id = 17 and post_id = 62. But postgres believes that there is.
推荐答案
其实我觉得这个问题根本不和postgres有关。它是同时重复的ajax调用的组合,创建将是重复记录,然后也不会插入。
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.
这篇关于本地postgres数据库不断给出错误重复键值违反唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!