我有一个OneToOne模型,我在单元测试中使用Factory。执行测试时,我得到一个外键约束违规: IntegrityError:在表格上插入或更新" core_designfeatures"违反外键约束
这在Django 1.9中不是问题,但出于其他原因我需要升级到1.10。
我有办法让这项工作成功吗?我在下面列出了相关的片段:
Django==1.10.5
factory-boy==2.8.1
Faker==0.7.7
模特摘录:
class DesignFeatures(BaseModel):
"""List of board features"""
design = models.OneToOneField(
Design,
on_delete=models.CASCADE,
related_name='features',
)
工厂摘录:
class DesignFeaturesFactory(factory.django.DjangoModelFactory):
class Meta(object):
model = models.DesignFeatures
class DesignFactory(factory.django.DjangoModelFactory):
class Meta(object):
model = models.Design
features = factory.RelatedFactory(DesignFeaturesFactory, 'design')
测试执行异常:
vi +216 /usr/local/lib/python2.7/site-packages/django/test/testcases.py # __call__
self._post_teardown()
vi +908 /usr/local/lib/python2.7/site-packages/django/test/testcases.py # _post_teardown
self._fixture_teardown()
vi +1064 /usr/local/lib/python2.7/site-packages/django/test/testcases.py # _fixture_teardown
connections[db_name].check_constraints()
vi +224 /usr/local/lib/python2.7/site-packages/django/db/backends/postgresql/base.py # check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
vi +112 /usr/local/lib/python2.7/site-packages/raven/contrib/django/client.py # execute
return real_execute(self, sql, params)
vi +64 /usr/local/lib/python2.7/site-packages/django/db/backends/utils.py # execute
return self.cursor.execute(sql, params)
vi +94 /usr/local/lib/python2.7/site-packages/django/db/utils.py # __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
vi +62 /usr/local/lib/python2.7/site-packages/django/db/backends/utils.py # execute
return self.cursor.execute(sql)
IntegrityError: insert or update on table "core_designfeatures" violates foreign key constraint "core_designfeatures_design_id_3cf84d33_fk_core_design_id"
DETAIL: Key (design_id)=(26) is not present in table "core_design".