0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

modelにカラムを追加したのにmigrationファイルに追加されない現象

Posted at

事象

models.pyに定義したモデルにカラムを追加した後,makemigrationを実行するもカラムが追加されない

原因

追加したカラムの末尾にカンマが存在しており正しく認識されていなかった模様

models.py
from django.db import models


class Category(models.Model):
    id = models.AutoField(primary_key=True)
    description = models.CharField(
        max_length=20
    )
    test_column1 = models.IntegerField()
    test_column2 = models.IntegerField(), # カンマがあるため反映されない

    class Meta:
        db_table = 'category'

python manage.py makemigrationsを実行すると

0001_initial.py
# Generated by Django 2.2.5 on 2019-10-21 07:51

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(primary_key=True, serialize=False)),
                ('description', models.CharField(max_length=20)),
                ('test_column1', models.IntegerField()),
            ],
            options={
                'db_table': 'category',
            },
        ),
    ]

たしかにtest_column2が反映されていないことが分かる.
makemigrations, migrateともに実行してもエラーや警告が発生せず原因を特定するのに時間が掛かってしまった.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?