foreign key constraint violation with RelatedFactory
See original GitHub issueI have a OneToOne model which I use with a Factory in unit tests. When tests are executed I get a foreign key constraint violation: IntegrityError: insert or update on table “core_designfeatures” violates foreign key constraint
This wasn’t a problem in Django 1.9, but I need to upgrade to 1.10 for other reasons.
Is there a way I can make this work? I included the relevant snippets below:
Django==1.10.5
factory-boy==2.8.1
Faker==0.7.7
Models excerpt:
class DesignFeatures(BaseModel):
"""List of board features"""
design = models.OneToOneField(
Design,
on_delete=models.CASCADE,
related_name='features',
)
Factories excerpts:
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')
Exception on test execution:
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".
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
faker - foreign key constraint violation with RelatedFactory ...
I have a OneToOne model which I use with a Factory in unit tests. When tests are executed I get a foreign key...
Read more >How can I troubleshoot an AWS DMS task that failed with a ...
I have an AWS Database Migration Service (AWS DMS) task that is failing with a foreign key constraint violation. Why did I receive...
Read more >foreign key constraint violation issue
It's a question of timing. Consider deleting StudentID #1: The row is deleted from the Student table; The cascade delete removes ...
Read more >Error 546
Foreign key constraint violation occurred, dbname = '%. ... Error 546 is raised when an integrity constraint is violated during execution of a...
Read more >Upgrade Fails due to Foreign Key Constraint Violation on ...
MySQLIntegrityConstraintViolationException : Cannot add or update a child row: a foreign key constraint fails (`conf31`.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I created a simple project to try to showcase the bug, but for whatever reason (maybe the libraries have changed a little) the bug doesn’t repro any more. Thanks for bearing with me and sorry for the false alarm. The code is here: https://github.com/mikebz/factory250
Thanks for following up!