django solves the problem of manage.py migrate being ineffective

  • 2020-10-23 20:09:07
  • OfStack

Problem description:

___, en__, and want to re-model the existing model. Delete the migrations folder with the exception of each ___.py makemigrations. It means 1 face is confused. Modify again, specify the table name, try again, and find the problem is still the same, indicating 2 confusion

The screening process

python manage.py dbshell go into the database and see if the table already exists

Result: The table does not exist

Check the migrations file

Results: The file is fine

Baidu google various search, random medical treatment, a variety of attempts

The solution

python manage py dbshell enter the database, execute delete from django_migrations where app='your_appname';

python manage.py makemigrations(do not perform this step if the migrations file is not deleted)

All right, we're done

Cause analysis,

View the django_migrations table structure

Construction sentence:


CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL); 

why

Failure of multiple application migrations reason is that the current model is modified, the original migrations has been I deleted, but increasing regenerated migrations using integer registered, so, in front of the django_migrations 0001000 2 in the table and so on several digital files have been recorded, in Django point of view, is recorded is equivalent to have used, so there will be a beginning of No migrations to apply.

Avoid scheme

Students with OCD who want to delete migrations file (such as me) should also delete the corresponding record in the database

If you don't have ocD, you can go on to generate new migrations and ignore the old one

digression

After executing python ES103en.py migrate, you can use python ES107en.py sqlmigrate migrations_num(for example, python manage py user 0002) to view the sql statement corresponding to the current migrations file.

In addition, when using the above command to view the sql statement in the 0002 file, django creates a new table, user_new, inserts the data in the user table, deletes the user table, and renames user_new to user. Therefore, when modifying model, you don't have to worry about losing the original data.


Related articles: