Backend unit test inside plugin: teardown shows django.db.utils.IntegrityError: insert or update on table "database_field" violates foreign key constraint

I would like to have a robust set of unit tests for my custom field types in my plugin. I have such a test however the “teardown” process throws an exception which i’ve yet to understand:

django.db.utils.IntegrityError: insert or update on table "database_field" violates foreign key constraint "database_field_content_type_id_3e7c32c9_fk_django_co"

Here’s the full stack trace:

/baserow/venv/lib/python3.9/site-packages/django/test/testcases.py:1006:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/baserow/venv/lib/python3.9/site-packages/django/test/testcases.py:1248: in _fixture_teardown
    connections[db_name].check_constraints()
/baserow/venv/lib/python3.9/site-packages/django/db/backends/postgresql/base.py:285: in check_constraints
    cursor.execute('SET CONSTRAINTS ALL IMMEDIATE')
/baserow/venv/lib/python3.9/site-packages/sentry_sdk/integrations/django/__init__.py:560: in execute
    return real_execute(self, sql, params)
/baserow/venv/lib/python3.9/site-packages/django/db/backends/utils.py:66: in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
/baserow/venv/lib/python3.9/site-packages/django/db/backends/utils.py:75: in _execute_with_wrappers
    return executor(sql, params, many, context)
/baserow/venv/lib/python3.9/site-packages/django/db/backends/utils.py:84: in _execute
    return self.cursor.execute(sql, params)
/baserow/venv/lib/python3.9/site-packages/django/db/utils.py:90: in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.backends.utils.CursorWrapper object at 0x7f799b8cb400>, sql = 'SET CONSTRAINTS ALL IMMEDIATE', params = None
ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f79e1943a60>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f799b8cb400>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                # params default might be backend specific.
>               return self.cursor.execute(sql)
E               django.db.utils.IntegrityError: insert or update on table "database_field" violates foreign key constraint "database_field_content_type_id_3e7c32c9_fk_django_co"
E               DETAIL:  Key (content_type_id)=(100) is not present in table "django_content_type".

/baserow/venv/lib/python3.9/site-packages/django/db/backends/utils.py:82: IntegrityError

Here’s the code for my unit test: baserow-vocabai-plugin/test_clt.py at main · Language-Tools/baserow-vocabai-plugin · GitHub

surprisingly, the issue doesn’t happen when I run a single test like this: pytest test_clt.py -k test_pinyin.

Could someone point me in the right direction ? Do I need to remove my custom created field types in order for the teardown process to work smoothly ?

Two suggestions (not tested):

  • Have you tried to decorate your test with @pytest.mark.django_db(transaction=True)
  • You can also try to add a models.py file that import and export (with __all__=["field1", "field2"]) in src/baserow_vocabai_plugin, may be the content_type module needs a models.py file to work properly?

May be you can remove some code in your test to locate exactly what’s actually triggering the error to have a more precise analysis?

That did it !! Many thanks @jrmi, you are becoming the expert problem solver in this forum !

1 Like