参考
履歴表示
マイグレーション履歴表示のコマンドは以下になります。
python manage.py showmigrations
例
$ 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 sessions [X] 0001_initial words [X] 0001_initial
マイグレーションファイル作成
マイグレーションファイルを作成するコマンドは以下になります。 対象アプリケーションのmodels.pyで、作成するテーブルの定義を追加したり、修正した後に、実行します。
python manage.py makemigrations [アプリケーション名]
例 プロジェクトstockにテーブルpricesを追加するマイグレーションファイル作成を実行した場合
$ python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, stock Running migrations: Applying stock.0002_prices... OK
例 マイグレーションファイル
# Generated by Django 3.1 on 2020-09-06 09:43
from django.db import migrations, models
class Migration(migrations.Migration):
    dependencies = [
        ('stock', '0001_initial'),
    ]
    operations = [
        migrations.CreateModel(
            name='Prices',
            fields=[
                ('id', models.IntegerField(primary_key=True, serialize=False)),
                ('brand_id', models.IntegerField(db_index=True)),
                ('trading_date', models.DateField(db_index=True)),
                ('opening', models.IntegerField()),
                ('closing', models.IntegerField()),
                ('high', models.IntegerField()),
                ('low', models.IntegerField()),
            ],
        ),
    ]
		
            
          マイグレーション実行
マイグレーションをDBに反映させるコマンドは以下になります。 プロジェクト以外のアプリケーションでは、アプリケーション名の指定が必要ですが、プロジェクトのマイグレーションの場合、アプリケーション名は省略できます。
python manage.py migrate [アプリケーション名]
$ python manage.py migrate words
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.2/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: words
Running migrations:
  Applying words.0001_initial... OK
  Applying words.0002_auto_20190507_2225... OK
		
            
          ロールバック
ロールバックのコマンドは以下になります。
python manage.py migrate [アプリケーション名] [マイグレーション名]指定したマイグレーション名が、最後の適用済みマイグレーションになるようにロールバックされます。
アプリケーションの全マイグレーションをロールバックする場合、マイグレーション名を"zero"にします。
python manage.py migrate [アプリケーション名] zero
例 wordsアプリケーションのマイグレーションを全て適用外にする
$ python manage.py migrate words zero
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.2/ref/databases/#mysql-sql-mode
Operations to perform:
  Unapply all migrations: words
Running migrations:
  Rendering model states... DONE
  Unapplying words.0001_initial... OK
  		
            
            履歴を確認すると以下のようになっています。
$ 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 ... ... sessions [X] 0001_initial words [ ] 0001_initial