Reputation: 5142
I've got a Django app with a lot of out-of-date migrations. I'd like to remove the old migrations and start fresh.
The app has 14 different "migrations" folders.
Here is what a few of them look like:
Is it safe to remove all the contents from each of these folders? Or, do I have to make sure to only remove some of the files -- and if so which files?
Upvotes: 21
Views: 43228
Reputation: 1
How to Reset Migrations
if you are using linux/unix os then you can fire this command. delete all migration directory.
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Upvotes: -2
Reputation: 949
You should never just delete migrations before unapplying them, or it will be a nightmare when you want to apply new migrations.
To unapply migrations you should do the following:
Use the python manage.py migrate your_app_name XXXX
in case you want to unapply migrations after the XXXX migration. Otherwise use python manage.py migrate your_app_name zero
to completely unapply all migrations.
Remove the .pyc
files under /migrations/_pycache_/ that you have unapplied.
Remove the .py
files under migrations/ that you have unapplied.
Now you can create new migrations without any headaches.
If what you're looking for is to squash all the migrations into one, do the steps above removing all migrations and then run python manage.py makemigrations your_app_name
to create a single migration file. After that just run python manage.py migrate your_app_name
and you're done.
Upvotes: 36
Reputation: 456
when you import from third app:
there are 2 step uninstall it
there are use the 'django_celery_beat' app for example.
python .\manage.py migrate django_celery_beat zero
there are done!!!
this is django document on this.
Upvotes: 0
Reputation: 5142
Having marked one of the answers provided previously as being accepted, here is a summary of a few things I learned:
migrate
that can be hard to fix.I was getting some of those hard-to-fix errors. Here is what I did to fix it:
migrate
on the production server.Migrate
then created the table, and then I repopulated it with data from the backup.Eventually I was able to complete all migrations successfully.
I have a feeling I lucked out and the above won't work in all cases! I've learned a lot about Django and migrations and will be much more careful about this in the future.
Upvotes: 0
Reputation: 2982
squash
them instead with squashmigrations
which reduces the files you have to two, init file and the initial migration file, this way your project still works.More to read at: https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-squashing
Upvotes: 2
Reputation: 15105
That depends. If you have a production database (or any database you cannot simply drop and recreate), then the answer is no, you cannot safely remove migrations.
If you do not have any permanent databases, then yes, you can remove all migrations, run python manage.py makemigrations --initial
and it will create fresh migrations based on your current models.
Also, you should check if any of the migrations are custom data migrations written by hand. If there are any, you might want to keep those.
The .pyc files are generally safe to remove, provided the related .py files are still there.
your first screenshot is not Django and looks like a JS project of some sort.
Upvotes: 7