Learning notes for the migrations feature in the Django framework (2)

Continuing from last time, I would like to continue studying the migrations feature of the Django framework.

Last synopsis

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

Outline of this time

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.

migrations related commands

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

Learning notes for the migrations feature in the Django framework (2)
Learning notes for the migrations feature in the Django framework (3)
Learning notes for the migrations feature in the Django framework (1)
Miscellaneous notes about the Django REST framework
List method for nested resources in Django REST framework
How to write custom validations in the Django REST Framework
Summary of stumbling blocks in Django for the first time
Launch notes for existing Django applications
Learning history for participating in team app development in Python ~ Django Tutorial 5 ~
Learning history for participating in team app development in Python ~ Django Tutorial 4 ~
Loop the For statement in reverse in an HTML file on Django
Learning history for participating in team app development in Python ~ Django Tutorial 1, 2, 3 ~
Switch the language displayed in Django 1.9
Learning history for participating in team app development in Python ~ Django Tutorial 6 ~
Learning history for participating in team app development in Python ~ Django Tutorial 7 ~
The meaning of ".object" in Django
Start Django for the first time
Summary of pages useful for studying the deep learning framework Chainer
Get the query string (query string) in Django
Get the client's IP address in Django
Logical deletion in Django, DRF (Django REST Framework)
Understand the benefits of the Django Rest Framework
Django ~ Let's display it in the browser ~
Notes on nfc.ContactlessFrontend () for nfcpy in python
Learning notes from the beginning of Python 1
Notes on creating static files in Django
Change the list in a for statement
Same-Site attribute setting for cookies in Django
Get query parameters for GET requests in Django
MongoDB for the first time in Python
Notes for using python (pydev) in eclipse
Learning notes from the beginning of Python 2
Try hitting the Spotify API in Django.
[Reinforcement learning] Search for the best route
Redo everything for the Django login screen
django notes
I tried to predict the change in snowfall for 2 years by machine learning
Feature engineering for machine learning starting with the 4th Google Colaboratory --Interaction features
Django notes
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
The _authenticate_with_backend function was obsolete in django auth.autenticate
The story of low learning costs for Python
Upgrade the Azure Machine Learning SDK for Python
Tips for hitting the ATND API in Python
Notes for implementing simple collaborative filtering in Python
Specify the view URL in your Django template
The story of viewing media files in Django
Implement JWT login functionality in Django REST framework
[Implementation for learning] Implement Stratified Sampling in Python (1)
[Django] css in the project cannot be read
Do an ambiguous search for mysql in Django
Knowledge notes needed to understand the Python framework
Implementing authentication in Django REST Framework with djoser
[For beginners] Introduction to vectorization in machine learning
I installed Chainer, a framework for deep learning
Let's reproduce the math teaching tool "Jamaica" ❗️ vol.02 "Notes for creating functions in Python"
Let's reproduce the math teaching tool "Jamaica" ❗️ vol.01 "Notes for displaying images in Python"