Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django deleted migrations directory

In my project directory while I was trying to update my code I have accidentally remove the migrations folder of my app, now that I have modified the models and when I use python manage.py makemigrations I get the following message:

Operations to perform:
    Apply all migrations: app_label
Running migrations:
   No migrations to apply.

I have already run this before migrating python manage.py makemigrations app_label

like image 748
samix73 Avatar asked Jun 02 '16 22:06

samix73


People also ask

What happens if I delete Django migrations?

Deleting migration files means losing your history. This historical info is recorded in the django_migrations table in your database. if you delete migration files, you will get dependency errors. So Don't try to lose your history by deleting your migration files.

Can I delete migration folder in Django?

Django will remove any prior migrations that were applied to the specified app (myApp). It searches for Python files (migration files) in the migrations folder of each project app, with the exception of init.py, and deletes them.

How do I restore migrations in Django?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .


2 Answers

You could just reset your migrations to before your initial migrate and start over. This doesn't delete data in the database, but rather resets the tracking of your migrations. If all of your databases are already migrated to the same level, then you can start over in your migration tracking.

There is a StackOverflow question here that already addresses it:

How to reset migrations in Django 1.7?

In short,

./manage.py migrate --fake <app-name> zero

This resets the tracking to prior to your initial migration.

And then,

./manage.py makemigrations <app-name>
./manage.py migrate <app-name>

Which recreates the initial migration and applies it.

As always, if your data is important, make a backup first.

like image 92
rrauenza Avatar answered Sep 18 '22 05:09

rrauenza


Just create new directory called migrations inside your app directory. Then inside that folder create empty folder named __pycache__ and a empty file named __init__.py. Then just make the migrations and migrate

like image 39
Dimitar Dimitrov Avatar answered Sep 20 '22 05:09

Dimitar Dimitrov