27👍
As others have stated, this is entirely the responsibility of the database.
But you should realize that this is the desirable behaviour. An ID uniquely identifies an entity in your database. As such, it should only ever refer to one row. If that row is subsequently deleted, there’s no reason why you should want a new row to re-use that ID: if you did that, you’d create a confusion between the now-deleted entity that used to have that ID, and the newly-created one that’s reused it. There’s no point in doing this and you should not want to do so.
32👍
I wouldn’t call it an issue. This is default behaviour for many database systems. Basically, the auto-increment counter for a table is persistent, and deleting entries does not affect the counter. The actual value of the primary key does not affect performance or anything, it only has aesthetic value (if you ever reach the 2 billion limit you’ll most likely have other problems to worry about).
If you really want to reset the counter, you can drop and recreate the table:
python manage.py sqlclear <app_name> > python manage.py dbshell
Or, if you need to keep the data from other tables in the app, you can manually reset the counter:
python manage.py dbshell
mysql> ALTER TABLE <table_name> AUTO_INCREMENT = 1;
The most probable reason you see different behaviour in your offline and online apps, is that the auto-increment value is only stored in memory, not on disk. It is recalculated as MAX(<column>) + 1
each time the database server is restarted. If the table is empty, it will be completely reset on a restart. This is probably very often for your offline environment, and close to none for your online environment.
- [Django]-Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7
- [Django]-Is there a HAML implementation for use with Python and Django
- [Django]-AttributeError: 'module' object has no attribute 'tests'
6👍
Did you actually drop them from your database or did you delete them using Django? Django won’t change AUTO_INCREMENT
for your table just by deleting rows from it, so if you want to reset your primary keys, you might have to go into your db and:
ALTER TABLE <my-table> AUTO_INCREMENT = 1;
(This assumes you’re using MySQL or similar).
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-Django FileField (or ImageField) open() method returns None for valid file?
- [Django]-Uwsgi throws IO error caused by uwsgi_response_write_body_do broken pipe
3👍
There is no issue, that’s the way databases work. Django doesn’t have anything to do with generating ids it just tells the database to insert a row and gets the id in response from database. The id starts at 1 for each table and increments every time you insert a row. Deleting rows doesn’t cause the id to go back. You shouldn’t usually be concerned with that, all you need to know is that each row has a unique id.
You can of course change the counter that generates the id for your table with a database command and that depends on the specific database system you’re using.
- [Django]-How do you join two tables on a foreign key field using django ORM?
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Get javascript variable's value in Django url template tag
3👍
If you are using SQLite you can reset the primary key with the following shell commands:
DELETE FROM your_table;
DELETE FROM SQLite_sequence WHERE name=’your_table’;
- [Django]-Laravel's dd() equivalent in django
- [Django]-Django form fails validation on a unique field
- [Django]-Django orm get latest for each group
2👍
Another solution for ‘POSTGRES’ DBs is from the UI.
Select your table and look for ‘sequences’ dropdown and select the settings and adjust the sequences that way.
example:
- [Django]-Django fix Admin plural
- [Django]-Django: Get model from string?
- [Django]-Django models: Only permit one entry in a model?
0👍
I’m not sure when this was added, but the following management command will delete all data from all tables and will reset the auto increment counters to 1.
./manage.py sqlflush | psql DATABASE_NAME
- [Django]-Django template system, calling a function inside a model
- [Django]-Represent Ordering in a Relational Database
- [Django]-Django – makemigrations – No changes detected