I accidentally deleted the migrations folder in Django

Viewed 3305

I am using Django, and Postgre for the DB. So I was done with all the migrations, but then I accidentally deleted the migrations folder in my editor. So I did the 'python manage.py makemigrations' again in the terminal, hoping I can get the folder back, but it replied 'No changes detected.' What should I do to get the folder back? Is it wrong to just simply make migrations again? I've tried creating a new database and re-do the same process, but it still says 'No changes detected.'

I very much appreciate your help. :)

5 Answers

If you have deleted all migrations then you have to reset the migrations and create again. don't worry, your DB will be safe.
Follow the below steps if you want to fix the migrations without loosing the database.

First Clear database migration history.

a. go to python shell python manage.py shell

b. type from django.db.migrations.recorder import MigrationRecorder

c. type MigrationRecorder.Migration.objects.all().delete()

Second, recreate migrations

Create a new folder migrations. Create a file named __init__.py inside the folder.

Run command python manage.py makemigrations.

Apply fake migration so your database schema and migration history sync.

python manage.py migrate --fake

I now found the solution on Django - makemigrations - No changes detected

The solution was:

python3 manage.py makemigrations yourAppName

This is for macOS.

If you use Windows it's probably

py manage.py makemigrations yourAppName

and "yourAppName" should be the name of your app, not the same as in the example, only if your apps name is actually "yourAppName", then of course that will work.

Try these following steps:

  1. clear pyc cache: run python manage.py clear_pyc
  2. Create new migrations folder if the old one is deleted and create an __init__.py file inside
  3. Create the migrations again by running python manage.py makemigrations <your-app-name>

Like that you will recover the deleted migration, as you can migrate afterwards everything by running python manage.py migrate

Delete everything under the migrations folder

and type the following command

python manage.py makemigrations

Related