django error to solve migration
- 1, Django understand the migration process
- Step 1: Generate migration files
- The second step: the implementation of the migration file
- Understand the relationship between migration and migration of records in the file
- 2, possible problems and solutions
- 2.1, first, the newly added field
- 2.2, the second, I do not know why migration is an error occurred (here only provides a solution to the problem of thinking)
- 3, other solutions
- 4, the above solutions are used, there is still a problem
1, Django understand the migration process
Step 1: Generate migration files
- 1 file code generation migration
- 2 migration file is located
- 3 migrate content
Migrating content of the document which records the contents of this migration, and inherited several times in front of Migration
(inheritance must be correct, for example, you delete one file migration, it could lead to incorrect inherit unable to perform the migration file)
The second step: the implementation of the migration file
Position 123 is a screenshot
- 1 migration code execution
- After 2 migrate record location data is being migrated
- 3 migration recorded content
Understand the relationship between migration and migration of records in the file
Look closely you will find that the migration file name and migration of records in the name is one to one
2, possible problems and solutions
2.1, first, the newly added field
If you added a new field in the model may occur while the migration code similar
to getting your choice, fill in the 1 or 2 on the line
- Option 1 : Let you write a default value for the new field (Enter option 1, then write a value then Enter ok, pay attention to the type of assignment corresponds to the type you set)
- Option 2 : will exit, then you need to re-models.py the corresponding field behind the new additions add a default = number, and then generate migrate again, to perform the migration, OK
2.2, the second, I do not know why migration is an error occurred (here only provides a solution to the problem of thinking)
First check the error reason, can not read English can be copied directly out of Baidu.
2.2.1, possible problems
- Migrating file inherits a problem
- Error pymysql.err.InternalError
- Error django.db.utils.InternalError
2.2.2 Solutions
① inconsistent migration to migrate files and records
- 1, check the migration to migrate files and records are the same (like me, like the screenshot below, the serial number should be consecutive)
- 2.1 (if more than the migration file records) to delete the migration file There is no record of re-generation and migration
- 2.2 (if recorded more than migrate files) to skip ①, ② direct execution
② to migrate files and records after migration agreement, or re-run error
如果①之后还是报错,再用②
- 1, models.py first reduced to modify the way before
- 2, and then delete records in the migration of files that are not (if not skip)
- 3, generate a new migration file, copy the new migration of operations to file operations on the back of a migrated file
- 4, after the migration copy the new files useless, delete
- 6, delete, add things you want to change or add to the models.py
- 5, regenerate re-run a new migration file (to most of the problems should have been solved here ②)
③ migration content recording and inconsistent database (which is mostly to manually change the configuration table inside the database, not by the models.py)
-
1, models.py check in code and database
mainly to see there are no more fields, field names are not the same and so is not -
2.1, if the poor things inside the database, the database which can be directly coupled to maintain consistent and models.py
-
2.2 If there models.py poor thing, started from the inside of ② 3
3, other solutions
python manage.py --fake [version name]
https://blog.csdn.net/a599174211/article/details/82795206
4, the above solutions are used, there is still a problem
这时候你可以选择暴力解决
1, the migration to migrate files and records are deleted
2, the construction of the data table are deleted (if the database content may need to insert a copy of the statement to keep in txt or direct export sql) (models.py inside content generally do not delete)
3, regenerate the migration file, re-run the migration file, and then check whether the migration of files and records the same, if there is no problem, you can insert the data into the database
生成迁移文件
python manage.py makemigrations
执行迁移文件,并记录
python manage.py migrate