The article directories

  • The migration command
    • Hoisting (Generate migration scripts)
      • Makemigrations parameters
    • Migrate (Map migration scripts into the database)
      • Migrate parameters
    • Showmigrations (View migration files)
    • SqlMigrate
  • Errors are common in migrations
    • django.db.utils.OperationalError: (1091, “Can’t DROP ‘***’; Check that the column/key exists “…
      • –fake
      • –fake-initial

In django. Db. Utils. OperationalError or other because accidentally deleted the mapping error caused by migration or records in the database can directly see the transition and version of the common error of migrations

The migration command

Executing the migration command must ensure that you are currently in the project directory (executelsCommand to see the manage.py file) and usePython manage.py migration commandCan. You can also create commands in a run/debug configuration if the IDE is PyCharm, all my migration commands below are done in PyCharm.

Hoisting (Generate migration scripts)

Names, Hoisting are commands we use many times, hoisting database models created in model.py to generate our database model librarymodel.pyChanges have taken place in the usepython manage.py makemigrationsA script is created in Migrations to record our changes

Makemigrations parameters

Names, Names, and Hoisting Will migrate the models in all APPS in the current project without parameters by default, and generate migration scripts. Names of database names will be generated based on changes in database names when few changes are made, and names of database names will be generated with the current time if too many changes are made.

Makemigrations parameters role
The name of the APP You can fill in one or more app names, and the migration script will only be generated for those apps.
- the name of the name Give the migration script a name.
--empty Generate an empty migration script. If you want to write your own migration script, you can use this command to implement an empty file,

For example, let me write down herepython manage.py makemigrations query --name add_VIPA migration script can be generated directly against query APP and named add_VIP

Migrate (Map migration scripts into the database)

Migrate is also a very common command that converts our generated mapping scripts into SQL statements to be executed in a specified database, where tables are generated or modified.

Migrate parameters

By default, Migrate maps the mapping scripts in all apps into the database

Migrate parameters role
The name of the APP Map migration scripts under one or more APP names to the database.
APP name mapping script Maps the specified mapping script in the named Migration folder under an app to the database.
--fake You can add the specified migration script name to the database. However, the migration script is not converted into SQL statements that modify the tables in the database.
--fake-initial Record the version number of the first generated migration file in the database. The migration script is not actually executed.

–fake and –fake-intial are used to resolve errors in migrated versions

Showmigrations (View migration files)

View migration files in an app. If there is no app behind, then all migration files in INSTALLED_APPS will be viewed. (This command is rarely used; most modern ides carry the current project directory.)

SqlMigrate

View the SQL statements converted when a migration file is mapped to the database

Python manage.py sqlMigrate APP Name Specifies the name of the APP migration scriptFor example, the fourth migration script name is shown below

0004_delete_text

In addition to writing the people, abbreviations into numbers can also be queried

Python manage.py sqlMigrate APP Name Specifies the number of the migration scriptThe number needs to be a full number, so the fourth number needs to be 0004, not 4

Errors are common in migrations

Django. Db. Utils. OperationalError: (1091, “Can ‘t DROP’ * * * ‘; Check that column/key exists”

– fake,

If you accidentally delete a mapping file, you may be able to map the script into the database again. How can you resolve this error using migrate--fakeForge a fake database file into the database

Suppose we have the current project as shown below, I have mapped the current project into the database, and everything is currently ok



If I accidentally delete the map 0003 in the database



When we execute it again, we find an errordjango.db.utils.OperationalError: (1091, "Can't DROP '***'; check that column/key exists")

In this case, our solution is to find the table that we do not match



And then executePython manage.py Migrate field --make APP name table nameFor example, what I’m doing here isMigrate --make field 0003_people_ Modify dateOr you can just do itpython manage.py migrate field --makeAll mappings that do not exist in the database are automatically added to the database.

– generic styles – initial

This is done if some mapping records are missing in the database, but what if some mapping files are missing in both your Django project and database, or something more complex happens that you need to use--fake-initialThis is the way to do it



In this case, we first delete all migration files (leaving __init__.py) from the migrations of the offending Django APP, and then place them in the databasedjango_migrationsDelete all records of the offending APP from the table.

And then this is the key stepThe ORM model (modls.py) of the APP with the problem should correspond to the fields in the database. If there is a mismatch, modify the models.py file, with more comments and less additions.



After the preceding operations are complete, perform the following operationsPython manage.py MakemigHoisting APP nameand

python manage.py migrate --fake-initial, and map it to the database.