Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all data for one and only one app in Django

Tags:

django

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…

like image 311
Sardathrion - against SE abuse Avatar asked Mar 06 '23 06:03

Sardathrion - against SE abuse


2 Answers

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

like image 118
Alberto Avatar answered Mar 16 '23 17:03

Alberto


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?

like image 33
Laurent S Avatar answered Mar 16 '23 18:03

Laurent S