[Django] Redo migrate

[Django] Redo migrate

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

Assumption

--Create app name "recipe" --Change user model to customized user model * --I want to redo migrate

Redo procedure

"I want to redo migrate" The procedure here is described below.

1. Check the past migrate history

#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.

2. Delete all tables

Delete (drop) all the tables generated by migrating in the past in the connection destination DB.

3. Delete the contents of the migrations folder of the app

[app name] Delete all the contents of the .migrations folder (In this case, recipe.migrations)

4. Delete migrate history

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.

5. Generate app migration file

Generate migration file as usual

>python manage.py makemigrations [app name]

6. Run migrate

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.

reference

Django Migration Summary I did something strange with manage.py migrate.

Recommended Posts

[Django] Redo migrate
Django
django update
Django note 4
Migrate Django applications running on Python 2.7 to Python 3.5
Django memorandum
django search
Django installation
Django Summary
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Redo everything for the Django login screen