[Fixed]-Django: new database ids shifted?

1👍

By default, the id is an integer that is always incremented at the creation of an object. It is also incremented such that ids of deleted objects are never used again.

The incrementation is handled by the database itself, not Django. For example, with PostgreSQL, the corresponding database field corresponding the “id” has the “PRIMARY KEY” constraint. It basically means that the field should be not null, and with no duplicates. Moreover the field will be associated with a sequence, that stores the id to use for the next row creation. To change this number, run this in the database shell:

ALTER SEQUENCE yourobjectstable_id_seq RESTART WITH 1234;

However, as emphasized in the comments to your question, this is something you should not do: it is better to keep the “uniqueness” feature of the primary key, even for deleted objects, since other tables may use the id to refer to a row in your main table.

👤mimo

Leave a comment