Reputation: 18246
I have a set up (Django 1.11) with several apps including OOK, EEK, and others irrelevant ones. I want to delete all the data for OOK while leaving EEK (and the rest) untouched. Ideally, I want all the primary keys to be reset as well so the first new OOK model will get 1 and so on…
Is this possible?
All I can find is reset
and sqlclear
which are both deprecated. flush
removed all data from the database and thus not what I want
I do release that this is an odd thing to do, but this is the hand given to me…
Upvotes: 5
Views: 2346
Reputation: 1408
You can achieve this behaviour dropping all the tables of that <app>
and then migrating only that <app>
. That way you'll reset the tables of the <app>
. In django 1.7+
you can do:
$ python manage.py migrate OOK zero //This command unapply your migrations
$ python manage.py migrate OOK
https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-migrate
Upvotes: 6
Reputation: 18246
This might not be possible with django. However, it is doable with raw SQL:
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE OOK_table1;
TRUNCATE OOK_table2;
[…]
SET FOREIGN_KEY_CHECKS = 1;
⚠ Do take a backup of your database before doing that!
Upvotes: 1
Reputation: 4326
If you are allowed to replace the db, you could export the data you need to a fixture, then do some clever text processing in the json that is in there, say by finding all ID
fields and replacing them from 1. Then reimport the result into a clean db?
The id
s are autoincremented by postgresql, according to this answer you can reset the index sequence, but not even sure it can go back to 1.
But really what's the point of resetting the indexes?
Upvotes: 1