3

I 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".
mikebz
  • 3,277
  • 8
  • 37
  • 50
  • 1
    Did you find a solution already? I'm facing the same issue. [To better catch bugs, `TestCase` checks deferrable database constraints at the end of each test in Django 1.10+.](https://docs.djangoproject.com/en/1.11/releases/1.10/#tests) Therefore the tests don't work any more. I thought a solution was to use `TransactionTestCase` (`TestCase` is a sub class) but I get a similar error, maybe because of a new behaviour described in [Miscellaneous](https://docs.djangoproject.com/en/1.11/releases/1.10/#miscellaneous) (point 12) of the release notes. – yofee Jul 02 '17 at 00:06
  • 3
    I just found out that I simply forgot to use `SubFactory` for a `ForeignKey` field. Adding it solved my issue. – yofee Jul 02 '17 at 01:10

0 Answers0