After deletion entries in your model relational database management systems not resetting autoincrementations and continue counting old IDs sequences. For start counting entries from 1 again you need reset sequence manually.
Django's autogenerated manage.py script have argument named "sqlsequencereset". It helps to reset autoincrement sequences for IDs in Django application after all instances in model are deleted. Documentation say:
django-admin sqlsequencereset app_label [app_label ...]
Prints the SQL statements for resetting sequences for the given app name(s).
Sequences are indexes used by some database engines to track the next available number for automatically incremented fields.
Use this command to generate SQL which will fix cases where a sequence is out of sync with its automatically incremented field data.
--database DATABASE
Specifies the database for which to print the SQL. Defaults to default.
Navigate in your application's workdir, activate virtual environment and run command for resetting sequences:
python manage.py sqlsequencereset <application_name> | python manage.py dbshell
Additionally PostgreSQL have equivalent command. Enter in psql
shell and run:
sudo -u postgres psql
postgres=# ALTER SEQUENCE <application_name>_<model_name>_id_seq RESTART WITH 1;
Are my posts is helpful? You may support me on Patreon.
Top comments (0)