python manage.py sqlsequencereset app | python manage.py dbshell
After that, the PK was still out of sync and I got PK violations. The
output from sqlsequencereset was:
BEGIN;
SELECT setval(pg_get_serial_sequence('"tablename"','id'),
coalesce(max("id"), 1), max("id") IS NOT null) FROM "tablename";
COMMIT;
After that I tried a different command which I wrote manually and worked.
SELECT setval('tablename_id_seq', (SELECT MAX(id) FROM tablename)+1)
It seems that the SQL produces by sqlsequencereset doesn't work on some
postgres versions.
Postgres Version 11.3
--
Ticket URL: <https://code.djangoproject.com/ticket/30681>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* version: 2.1 => master
* resolution: => worksforme
Comment:
Thanks for this report, however it works for me. Moreover a related test
[https://github.com/django/django/blob/25f21bd2376603c8e233a0a0e5a726a0fdfdd33e/tests/backends/tests.py#L172-L188
SequenceResetTest.test_generic_relation] works on PostgreSQL 11. Can you
provide more details? or an example project to reproduce this issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/30681#comment:1>
* status: closed => new
* resolution: worksforme =>
Comment:
I find the same results on PostgreSQL 13 (Debian 11 bullseye) with PostGIS
extension.
After migrating my database from another server with pg_dump/psql, I found
that some sequences were no longer in order.
Using ./manage.py sqlsequencereset myapp | psql did not work and I found
that some guides tell you to:
`SELECT setval('load_load_id_seq', (SELECT MAX(load_id) FROM load));` (in
my case)
instead of `pg_get_serial_sequence()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/30681#comment:2>
* status: new => closed
* resolution: => worksforme
Comment:
> After migrating my database from another server with pg_dump/psql, I
found that some sequences were no longer in order.
`pg_get_serial_sequence()` should work, you dumped and loaded a database
outside of Django, so it's not an issue in Django itself.
--
Ticket URL: <https://code.djangoproject.com/ticket/30681#comment:3>