Throws an exception due to the following SQL error: duplicate key
violates unique constraint.
The application was working fine before and suddenly gives this
grief. I have upgraded all my gems
so I’m now running Rails 1.1.2 and the problem is still there.
I think the requirement to manually reset the index is a bug in
postgresql. Either in
implementation or design. Restoring from a dump should restore the
indexes to a working
state. Perhaps pg_dump has an option to write a dump file that restores
the index? I had
the same problem and filed a bug report at postgresql against 8.1.3.
The database should
not be in an inconsistent state. MySQL does not screw up it indexes
after a restore, you
can insert right away.
Robby’s solution worked for me. Still, we shouldn’t have to do it.
I took a look at the SQL generated by pg_dump. I think the problem is
that the sequence values are being dumped and restored … instead of
just being ignored.
So if you do a restore, but have munged the backup file then there is
a problem if you don’t sync the sequence bits as well.
Am sure I missed something, but that’s what is immediately apparent …
I ran into this issue when I backuped a database and restored it back
into another system.
You might want to look into how that database got backed up. By default
(at least every where I’ve done it) pg_dump will include the statements
necessary to “reset” the sequences… that is, all my dumps have lines
like this:
SELECT MAX(id) FROM entities;
Then run…
This should be higher than the last result.
If it’s not higher… run this to try and fix it. (run a quick
pg_dump first…)
SELECT setval(‘entities_id_seq’, (SELECT MAX(id) FROM entities)+1);
reload your app…and see if its still happening.
I have the same problem but I don’t know how to solve it since I can’t
relate the solution here (names of tables, etc.) to the data given by
the OP. How do you know you need to SELECT MAX(id) FROM entities, or
SELECT setval(‘entities_id_seq’, (SELECT MAX(id) FROM entities)+1);
with the information given by the OP? I can’t see the relation and
therefore I have no clue how I can solve my problem.
SELECT setval(‘entities_id_seq’, (SELECT MAX(id) FROM entities)+1);
reload your app…and see if its still happening.
Good luck!
-Robby
I don’t code with Ruby but i registered only to say THANK YOU Robby you
saved my day.
Hi,
I am writing below statement in pgsql file in function
endpoint_to_dyninfo_func()
SELECT setval(‘endpoints_dyninfo_id_seq’, (SELECT MAX(id) FROM
endpoints_dyninfo));
But i am gettign error if i am compiling using user msw as,
psql -Umsw < all_trigger_func.pgsql
ERROR: syntax error at or near “endpoints_dyninfo_id_seq”
LINE 22: SELECT setval(‘endpoints_dyninfo_id_seq’, (SELECT MAX(…
^
ERROR: function endpoint_to_dyninfo_func() does not exist
Same statement i can use using below steps.
1> Ratnesh-alt:/databases/databases/pg_log # psql -Umsw
2>
msw=# SELECT setval(‘endpoints_dyninfo_id_seq’, (SELECT MAX(id) FROM
endpoints_dyninfo));
setval
1326
(1 row)
Please help asap.
Thanks,
Ratnesh
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.