Django 1.10.5:关系'myapp_mymodel'即使在迁移后也不存在

时间:2017-02-15 16:59:47

标签: python django postgresql

我在model.py中有一个django app“myapp”:

在同一文件夹中,我有一个migrations文件夹,其中包含空__init__.py和2个文件:

from django.db import models
from django.contrib.postgres.fields import ArrayField

class Characteristic(models.Model):
    name = models.CharField(max_length=200)
    core = models.BooleanField(default=False)
    synonyms  =  ArrayField(
        models.CharField(max_length=200, blank=True),
        size=20
        )

0001_initial.py

from __future__ import unicode_literals

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='characteristics',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
                ('synonyms', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=200), size=20)),
            ],
        ),
    ]

第二档:

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

    operations = [
        migrations.AddField(
            model_name='characteristics',
            name='core',
            field=models.BooleanField(default=False),
        ),
    ]

但是,在视图中,当尝试创建模型的实例时:

characteristic = Characteristic(name=attribute.decode('utf-8'), synonyms=[])
characteristic.save()

我收到错误:

ProgrammingError: relation "myapp_characteristic" does not exist

我做过:python manage.py makemigrations myapp我位于文件顶部from myapp.models import Characteristic

有谁知道我哪里错了?

1 个答案:

答案 0 :(得分:1)

Makemigrations命令只是为数据库中的更改创建迁移。它确实在DB中执行任何类型的修改。

python manage.py migrate

上面的命令在数据库中进行实际修改并在迁移表中创建一个条目。这样Django就可以了解实际迁移了哪些迁移