This is a memo for Django beginners. There was a time when I wanted to rewrite the Model and redo the migrate when building the DB, but I had a hard time with various errors, so I will make a note of the solution.
[environment] Django 2.01 python 3.7.2
--Create app name "recipe" --Change user model to customized user model * --I want to redo migrate
"I want to redo migrate" The procedure here is described below.
#You can see the past migrate history
>python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
recipe
[X] 0001_initial
sessions
[X] 0001_initial
The result is as above, and the one with x in [] is the history of migrte being applied. Since the recipe also has [x], it can be judged that it has been adapted.
Delete (drop) all the tables generated by migrating in the past in the connection destination DB.
[app name] Delete all the contents of the .migrations folder (In this case, recipe.migrations)
Delete the history with the following command with the --fake option. (When re-adapting the customized User, it was not solved unless all were deleted, so I deleted everything from admin to sections.)
>python3 manage.py migrate --fake [Target to be deleted] zero
#Example:This will delete the recipe history
#>python3 manage.py migrate --fake recipe zero
Check if the history has been deleted with the same command as 1.
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
[ ] 0003_logentry_add_action_flag_choices
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length
[ ] 0010_alter_group_name_max_length
[ ] 0011_update_proxy_permissions
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
recipe
[ ] 0001_initial
sessions
[ ] 0001_initial
Make sure all history has been deleted.
Generate migration file as usual
>python manage.py makemigrations [app name]
Run migrate as usual
>python manage.py migrate
Now check the DB and make sure the table is generated.
That's it. If you have any mistakes, please let us know.
Django Migration Summary I did something strange with manage.py migrate.
Recommended Posts