Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django makemigrations wants to delete everything it just created

I just followed this procedure:

  • makemigrations (success)
  • migrate (success)
  • Copy the app on another server (with the migration files)
  • Create a new empty database on that server
  • migrate (success, it creates the correct schema)
  • Fill the new database with data
  • Just to test: migrate ....

At this point Django says I have "changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them"

But when I run makemigrations, it creates a new one that wants to "Remove field" every foreign key and "Delete model" all of my models. If I run it it empties my database. My models.py are intact.

What is happening ??

like image 274
JulienD Avatar asked Oct 13 '25 12:10

JulienD


1 Answers

I had the same problem in my project. Running forward I can say that django removes models that has no import (making the Delete model migration). The documentation says that you should import your model in the myApp/models/__init__.py file (see https://docs.djangoproject.com/en/1.11/topics/db/models/#organizing-models-in-a-package).

In my case I imported the model somewhere to make manipulations but model had been removing elsewhere.

I had made a useless import in an admin.py file that has solved my situation (I didn't try to follow the documentation and import it in the __init__.py but sure it should help). I had not realized yet why does it work that way (hope that someone could note this moment) and also hope this solution will help you.

like image 73
A. Grinenko Avatar answered Oct 15 '25 19:10

A. Grinenko