Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Migration is not applying the migration changes

Tags:

Using django 1.7.7 I want to use django's migration to add or remove a field. so I modified model.py and ran

python manage.py makemigrations myproj Migrations for 'myproj':   0001_initial.py:     - Create model Interp     - Create model InterpVersion  python manage.py migrate myproj Operations to perform:   Apply all migrations: myproj Running migrations:   Applying myproj.0001_initial... FAKED  python manage.py runserver 

Then checked the admin page, it is not updated. Then I tried removing the migration folder and tried again; the migrate command says there are no migrations to apply.

How can I do the migration? Note: I want to use the new technique using django's migration not the old south approach.

like image 282
max Avatar asked Apr 27 '15 17:04

max


People also ask

How does Django detect migration changes?

Django looks for changes made to the models in your app <appname> . If it finds any, like a model that has been added, then it creates a migration file in the migrations subdirectory. This migration file contains a list of operations to bring your database schema in sync with your model definition.

Why Makemigrations is not working?

This may happen due to the following reasons: You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS.


2 Answers

Make sure that the migrations/ folder contains a __init__.py file

Lost half an hour over that.


Deleting the migration directory is never a good idea, because Django then loses track of which migration has been applied and which not (and once an app is deployed somewhere it can become quite difficult to get things back in sync).

Disclaimer: whenever things like that occur it is best to backup the database if it contains anything valuable. If in early development it is not necessary, but once things on the backend get out of sync there is a chance of things getting worse. :-)


To recover, you could try resetting your models to match exactly what they were before you have added/removed the fields. Then you can run

$ python manage.py makemigrations myproj 

which will lead to an initial migration (0001_initial...). Then you can tell Django to fake that migration, which means to tell it to set its internal counter to this 0001_initial:

With Django 1.7:

$ python manage.py migrate myproj 

With Django >= 1.8:

$ python manage.py migrate myproj --fake-initial 

Now, try to change your model and run makemigrations again. It should now create a 0002_foobar migration that you could run as expected.

like image 39
sthzg Avatar answered Oct 18 '22 16:10

sthzg