I do not understand why postgres raises:
duplicate key value violates unique constraint
I went to check the table in pgadmin to see if the table really had a duplicate and see:
VACUUM recommended
The estimated score for a row in a table is significantly different from the actual row.
Why is this happening? Fortunately, this does not happen in production on the hero. This application is for rails.
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, Mar 18, 2012 03:48:37 UTC +00: 00], ["post_id", 62], ["updated_at", Sun, Mar 18, 2012 3:48:37 UTC +00: 00]] PG :: Error: ERROR: duplicate key value violates unique constraint " index_favorites_on_artist_id_and_post_id "DETAILS: Key (artist_id, post_id) = (17, 62) already exists.
But in a real table there is no such record with artist_id = 17 and post_id = 62. But postgres thinks there is.
source share