Continuing from last time, I would like to continue studying the migrations feature of the Django framework.
--Use the Djange framework to create a project, create a test app, and complete the database setup --Initialize and create a model file for the app and reflect it in the database --Add a new field to the model, reflect it in the database --Restore the database to its initialized state to undo the changes in the steps above
Development environment |
---|
Mac OS:Sierra |
python2.7.10 |
django1.11.2 |
mysql5.7.18 |
Last time, I introduced the basic workflow of migrations, but this time I will study basic commands related to migrations and how to solve problems that you often encounter during actual use.
The Official Documents introduces four commands related to migrations. Let's take advantage of the demo app and use commands.
showmigrations Enter the following command on the command line:
python manage.py showmigrations
In the output:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
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
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
polls
[X] 0001_initial
[ ] 0002_article_image_url
sessions
[X] 0001_initial
The current status of migrations for each app in the project is displayed in a checklist. All default app migrations such as admin, auth, contenttypes and sessions are reflected. In polls, which is a demo app, the migrations file 0001_initial.py reflected in the initialization is checked. The migrations file 0002_article_image_url.py, which was canceled by the recovery, was unchecked. You can also specify the app with options in the command.
python manage.py showmigrations polls
This will narrow the output to a specific app.
polls
[X] 0001_initial
[ ] 0002_article_image_url
makemigrations Create a migrations file according to the model change, see Last time for a concrete example.
migrate Use the created migrations file to reflect the model in the database. Let's add the canceled changes again.
python manage.py migrate polls
output
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0002_article_image_url... OK
Check the status of migrations in the demo app:
python manage.py showmigrations polls
output:
polls
[X] 0001_initial
[X] 0002_article_image_url
You can see that the migrations file 0002_article_image_url.py has been reflected.
sqlmigrate
Shows the sql statement that actually executes the contents of the migrations file. If you want to use it, you need to put the name of the target application and the migrations file name in the options. Let's specify the migrations file 0002_article_image_url and execute it.
python manage.py sqlmigrate polls 0002_article_image_url
output
BEGIN;
--
-- Add field image_url to article
--
ALTER TABLE `polls_article` ADD COLUMN `image_url` varchar(200) DEFAULT toBeImplement NOT NULL;
ALTER TABLE `polls_article` ALTER COLUMN `image_url` DROP DEFAULT;
COMMIT;
The behavior of suspicious migrations has been cleared at once. In addition, with the --backwards option, you can also see the sql statement to cancel migrations.
python manage.py sqlmigrate --backwards polls 0002_article_image_url
output
BEGIN;
--
-- Add field image_url to article
--
ALTER TABLE `polls_article` DROP COLUMN `image_url`;
COMMIT;
The comment describes the migrations file, but the sql statement properly undoes the changes (removes the image_url column).
Recommended Posts