Ryan Oh
Ryan Oh

Reputation: 657

I accidentally deleted the migrations folder in Django

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. :)

Upvotes: 8

Views: 8287

Answers (6)

Rahul Shukla
Rahul Shukla

Reputation: 91

(if not in production and afford to loose data)

  1. Simply delete the migration Folder from your app directory, delete *.sqlite3 file
  2. python manage.py makemigrations
  3. python manage.py migrate

You will get a new database file and with fixed migration.

Happy Django user !!

Upvotes: 1

AnonymousUser
AnonymousUser

Reputation: 786

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.

Upvotes: 1

Ajay saini
Ajay saini

Reputation: 2470

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

Upvotes: 16

nigel222
nigel222

Reputation: 8222

I have found this recipe (Scenario 2, to keep the existing development DB) works well. It can be applied to a single app, which is possibly trickier than resetting migrations on all apps at once.

Upvotes: 0

SOF
SOF

Reputation: 357

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

Upvotes: -1

Zafer Cengiz
Zafer Cengiz

Reputation: 9

Delete everything under the migrations folder

and type the following command

python manage.py makemigrations

Upvotes: -2

Related Questions