我正在开发一个Django项目,并使用相同模型的几个models.ForeignKey实例创建了一个模型。
class Country(models.Model):
name = models.CharField(max_length=100)
primary_language = models.ForeignKey('Language', related_name='primary_language', default="")
secondary_language = models.ForeignKey('Language', related_name='secondary_language', default="")
tertiary_language = models.ForeignKey('Language', related_name='tertiary_language', default="")
def __str__(self):
return self.name
这是语言模型:
class Language(models.Model):
name = models.CharField(max_length=50)
abbreviation = models.CharField(max_length=2)
def __str__(self):
return self.name
在执行$python3 manage.py makemigration base
时,它运行正常,没有错误。
我已经把我认为最重要的2个迁移文件放了。
class Migration(migrations.Migration):
dependencies = [
('base', '0002_country_country_code'),
]
operations = [
migrations.CreateModel(
name='Currency',
fields=[
('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
('name', models.CharField(max_length=50)),
('abbreviation', models.CharField(max_length=3)),
],
),
migrations.CreateModel(
name='Language',
fields=[
('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
('name', models.CharField(max_length=50)),
('abbreviation', models.CharField(max_length=2)),
],
),
migrations.AddField(
model_name='country',
name='phone_country_code',
field=models.CharField(default='', max_length=7),
),
migrations.AlterField(
model_name='country',
name='country_code',
field=models.CharField(default='', max_length=2),
),
migrations.AddField(
model_name='country',
name='primary_language',
field=models.ForeignKey(to='base.Language', default=''),
),
migrations.AddField(
model_name='country',
name='secondary_language',
field=models.ForeignKey(related_name='secondary_language', to='base.Language', default=''),
),
migrations.AddField(
model_name='country',
name='tertiary_language',
field=models.ForeignKey(related_name='tertiary_language', to='base.Language', default=''),
),
]
class Migration(migrations.Migration):
dependencies = [
('base', '0006_auto_20151023_0918'),
]
operations = [
migrations.AddField(
model_name='country',
name='primary_language',
field=models.ForeignKey(default='', related_name='primary_language', to='base.Language'),
),
migrations.AddField(
model_name='country',
name='secondary_language',
field=models.ForeignKey(default='', related_name='secondary_language', to='base.Language'),
),
migrations.AddField(
model_name='country',
name='tertiary_language',
field=models.ForeignKey(default='', related_name='tertiary_language', to='base.Language'),
),
migrations.AlterField(
model_name='language',
name='abbreviation',
field=models.CharField(max_length=2),
),
migrations.AlterField(
model_name='language',
name='name',
field=models.CharField(max_length=50),
),
]
现在,在运行迁移时,我收到一条错误消息,我无法弄清楚。 我认为这些是堆栈跟踪中重要的行:
johan@johan-pc:~/sdp/gezelligehotelletjes_com$ python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: auth, base, sessions, admin, contenttypes, hotel
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying base.0003_auto_20151023_0912...Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "primary_language_id" contains null values
The above exception was the direct cause of the following exception:
django.db.utils.IntegrityError: column "primary_language_id" contains null values
首先,我没有专栏" primary_language_id"但我猜这是由Django创建的。即使删除整个语言模型和Country模型中的语言行,我仍然会收到此错误。
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
您的数据库中已有Country
个对象。
当您向其添加primary_language_id
列(代表primary_language
ForeignKey
)时,这些国家/地区最终会显示空primary_language
(因为您没有' t指定默认值),这会引发错误(因为您不允许primary_language
的空值。)
解决方案取决于您希望迁移如何工作。您可以将blank = True
添加到primary_language
ForeignKey
定义,添加默认设置,或者在3次迁移中细分迁移(添加blank = True
列,设置值,删除blank = True
)。