Django数据库没有迁移

时间:2017-09-29 12:54:27

标签: python mysql django

我使用Django 1.10

学校/ models.py

class School(models.Model):
    name = models.CharField(max_length=10,null=False)
    school_id = models.CharField(max_length=8,null=False)
    weather = models.ForeignKey(Weather,related_name="school")

当我通过添加eng_name = models.CharField(max_length=50,null=False)

修改学校课程时

我先删除了迁移文件。

运行命令后:

python manage.py makemigrations school

python manage.py migrate --fake-initial school

python manage.py migrate school

migrate的消息都是

Operations to perform:
  Apply all migrations: school
Running migrations:
  No migrations to apply.

但是数据库没有更新。

导致此问题的可能原因是什么?

UPDATAE

迁移/ 0001_initial.py

class Migration(migrations.Migration):

initial = True

dependencies = [
    ('weather', '0001_initial'),
]

operations = [
    migrations.CreateModel(
        name='School',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('name', models.CharField(max_length=10)),
            ('eng_name', models.CharField(max_length=50)),
            ('school_id', models.CharField(max_length=8)),
            ('weather', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='school', to='weather.Weather')),
        ],
    ),
]

python manage.py showmigrations --plan

  

[X] weather.0001_initial

     

[X] school.0001_initial

这两者都是最新的迁移。

1 个答案:

答案 0 :(得分:0)

Django将应用的迁移存储在数据库中作为表(django_migrations)与列(id,app,name,applied)。只需删除已应用“学校”应用初始迁移的行,然后进行迁移。并且,将来不要删除旧的事务文件。有了它们,django将为迁移机制创建正确命名的事务。