5

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…

6
  • As far as I know, handing out ids, is something that happens at the database layer, hence Django has not much to say on that. You can do this with raw queries, but that is probably not a good idea. Commented May 10, 2018 at 8:02
  • The easiest way is using python manage.py flush you could read the documentation docs.djangoproject.com/en/1.11/ref/django-admin/#flush Commented May 10, 2018 at 8:02
  • @WillemVanOnsem I get that but those are the requirements I have to work under. I am happy to mess with raw SQL and clearly will test this before doing it on the main database which will be backed up. Commented May 10, 2018 at 8:07
  • @Sardathrion my mistake I didn't read it correctly. So in my ears, it sounds like non-possible but I will be waiting for an answer by someone more relevant in this field. Commented May 10, 2018 at 8:09
  • @Sardathrion: well for MySQL you can reset the index with stackoverflow.com/a/8923132/67579 Commented May 10, 2018 at 8:09

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Would you not lose all data for OOK by doing this? Unless you have migrations for data as well...
Yes, but that's what he asked for "...I want to delete all the data for OOK...". Didn't he?
It's dangerous, because it can lead to unapplying migrations in other apps!
Also unfortunately it does not work if your data migrations don't have reverse functions. But it's not a big deal to add dummy reverse lambda-functions to data migrations.
1

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 ids 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?

1 Comment

Some of the indexes are used as serial numbers for items. Manager wanted them back to 1 or 1000 whatever was easiest.
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.