Thursday, 1 November 2012

ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint

ActiveRecord::RecordNotUnique (PG::Error: ERROR:  duplicate key value violates unique constraint

ActiveRecord::RecordNotUnique: PG::Error: ERROR: duplicate key value violates unique constraint "users_pkey" DETAIL: Key (id)=(143) already exists.

The above issue will raise when you dump data to database and start creating records after that. You need to set the sequence value manually to over come this issue. Below is the command to set the value for 'users_id_seq'


SELECT setval('users_id_seq', (SELECT max(id) FROM users));

In the above query 'users' is the tale and you are manually changing the 'users_id_seq' value to max(id) of users.


Thank You,
Uma Mahesh.

2 comments:

  1. I also got the same error. But Uma, your blog help me to solve the issue.
    Keep writing on rails.
    Thanks,
    Sagar
    (sagarjunnu@gmail.com)

    ReplyDelete