Django Migration does not apply migration changes

Using django 1.7.7 I want to use django wrapping to add or remove a field. so I changed 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 the admin page is checked, it is not updated. Then I tried to delete the transfer folder and tried again; the migrate command says no migrations are required.

How can I migrate? Note. I want to use a new technique using django migration, not the old sub approach.

+7
django
source share
5 answers

Removing the migration directory is never a good idea, as Django then loses track of which migration has been applied and which has not (and once the application has been deployed somewhere, it can be quite difficult to restore synchronization).

Disclaimer : whenever such things arise, it is best to backup the database if it contains anything of value. If in the early development this is not necessary, but as soon as things on the backend go out of sync, there is a possibility that the situation will worsen .:-)


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

 $ python manage.py makemigrations myproj 

which will lead to the initial migration ( 0001_initial... ). You can then tell Django that this is a migration, which means that he must set his internal counter for 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 changing the model and run makemigrations again. Now it should create a 0002_foobar migration, which you could run as expected.

+14
source share

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

Lost in half an hour.

+4
source share

In my case, the migrations were not reflected in the mysql database. I manually deleted the string "myapp" (in your case "myproj") from the table "django_migrations" in the mysql database and again executed the same commands for the migration.

+2
source share

I believe that Django migrations are a bit of a mystery and tend to prefer external tools (like Liquibase).

However, I just ran into the "No migration to apply" problem. I also tried deleting the migrations folder, which does not help.

If you already deleted the migrations folder, here was an approach that worked for me.

First create 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 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 the execution of the same SQL in your database.

I use Postgres, but it will be similar to other engines.

One way is to write the contents to a file:

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

Another is a direct SQL query:

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

Or copy and paste it, etc.

0
source share

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

0
source share

All Articles