'Django Migration is not applying the migration changes

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.



Solution 1:[1]

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

Lost half an hour over that.

Solution 2:[2]

In my case, the migrations were not being reflected in mysql database. I manually removed the row of 'myapp'(in your case 'myproj') from the table 'django_migrations' in mysql database and ran the same commands again for migration.

Solution 3:[3]

Most of the above solutions would help in the issue, however, I wanted to point out another possible (albeit rare) possibility that the allow_migrate method of database router may be returning False when it should have returned None.

Django has a setting DATABASE_ROUTERS which will be used to determine which database to use when performing a database query.

From the docs:

if you want to implement more interesting database allocation behaviors, you can define and install your own database routers.

A database router class implements up to four methods:

  • db_for_read(model, **hints)
  • db_for_write(model, **hints)
  • allow_relation(obj1, obj2, **hints)
  • allow_migrate(db, app_label, model_name=None, **hints)

From the documentation:

allow_migrate(db, app_label, model_name=None, **hints)

Determine if the migration operation is allowed to run on the database with alias db. Return True if the operation should run, False if it shouldn’t run, or None if the router has no opinion.

It is possible that one of the database routers in sequence is returning False for the migration that you're trying to run, in which case the particular operation will not be run.

Solution 4:[4]

I find Django migrations a bit of a mystery, and tend to prefer external tools (liquibase, for example).

However, I just ran into this "No migrations to apply" problem as well. I also tried removing the migrations folder, which doesn't help.

If you've already removed the migrations folder, here's an approach that worked for me.

First, generate the new "clean" migrations:

$ python manage.py makemigrations foo
Migrations for 'foo':
  dashboard/foo/migrations/0001_initial.py
    - Create model Foo
    - Create model Bar

Then look at the SQL and see if it looks reasonable:

$ python manage.py sqlmigrate foo 0001
BEGIN;
--
-- Create model Foo
--
CREATE TABLE "foo" ("id" serial NOT NULL PRIMARY KEY, ... "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL);
CREATE INDEX "..." ON "foo" (...);
COMMIT;

Then apply execute that same SQL on your database.

I'm using Postgres but it will be similar for other engines.

One way is to write the content to a file:

$ python manage.py sqlmigrate foo 0001 > foo.sql
$ psql dbname username < foo.sql
BEGIN
CREATE TABLE
CREATE INDEX
COMMIT

Another is pipe the SQL directly:

$ python manage.py sqlmigrate foo 0001 | psql dbname username

Or copy and paste it, etc.

Solution 5:[5]

In addition to the other answers, make sure that in models.py, you have managed = True in each table's meta

Solution 6:[6]

pip install django-extensions

and add this in the install app of settings.py

INSTALLED_APPS = [
    'django_extensions'
]

Then run

python ./manage.py reset_db

Then run migrations again

python manage.py makemigrations
python manage.py migrate

Now, run migrations for your installed apps

python manage.py makemigrations your_app_name
python manage.py migrtate your_app_name

Done! See Your Database...

Solution 7:[7]

You can remove your db

  • python manage.py makemigrations
  • python manage.py migrate
  • python manage.py migrate --run-syncdb

and see your data base this is working :)

Solution 8:[8]

Similar to Andrew E above but with a few changes especially where you haven't deleted the migrations folder in your quest to resolve the issue

1 - In your intact migration folder just examine the 000*.py files counting from the highest down to initial.py till you find the one where your Model is defined, say 0002_entry.py

2 - python manage.py sqlmigrate app-name 0002 > 0002_sql.txt to capture the SQL commands

3 - Edit this file to ensure there are no hard CR/LFs and the ALTER, CREATE INDEX commands are each on own single line

4 - Log into your DB (I have Postgres) and run these commands

Solution 9:[9]

In Database delete row 'myproj' from the table 'django_migrations'. Delete all migration files in the migrations folder. Then run python manage.py makemigrations and python manage.py migrate command.