我在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
有谁知道我哪里错了?
答案 0 :(得分:1)
Makemigrations命令只是为数据库中的更改创建迁移。它确实在DB中执行任何类型的修改。
python manage.py migrate
上面的命令在数据库中进行实际修改并在迁移表中创建一个条目。这样Django就可以了解实际迁移了哪些迁移