SoFunction
Updated on 2024-11-16

About django database migration (migrate) should know some things

command

First the two main commands for database migration:

python  makemigrations & python  migrate 

The former turns the model layer into a migration file migration, and the latter executes the new version of the migration file and updates the database.

Both of these command calls are global by default, i.e. they operate on all the latest changes to the mod or migration files. If you want to work on some apps, you have to append the app name to it:

$ python  makemigrations app_name
$ python  migrate app_name

If you want to be precise to a certain migration file (0004_xxx.py):

$ python  migrate app_name 0004

If you want to see the execution status of the migration file, you can view it with the showmigrations command:

$ python  showmigrations
admin
 [X] 0001_initial
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length

Displays the known migrations and status of django.

incorrect

Database commands can fall into a pit if you are not careful. In particular, the migrate command, since django's database contains records of migrations, if the migrations file is missing, it is likely to cause migrate to fail. So it is necessary to add the migrations file to the version control, to ensure that the development of the migrations record and file match.

If migrate fails, it is likely because the migration file contains changes that cannot be completed due to current database constraints. You should find the location of these records or keys in the data, delete them and redo the process.

Generally these data exist in the tables corresponding to foreign key constraints, auth_permission, django_content_type and django_migrations.

Above this article on django database migration (migrate) should know some things is what I share with you all, I hope to give you a reference, but also hope that you support me more.