﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29948	Single empty TransactionTestCase fails with IntegrityError	Matt Westcott	nobody	"https://github.com/django/django/commit/b3b1d3d45fc066367f4fcacf0b06f72fcd00a9c6 (#25251) appears to have introduced a regression where a TransactionTestCase consisting of a single empty test fails on teardown with an IntegrityError.

To reproduce:

* start a new project (`django-admin startproject testproject ; cd testproject`)
* add a new app (`./manage.py startapp foo`, add `'foo'` to INSTALLED_APPS)
* change foo/tests.py to the following:

{{{
from django.test import TransactionTestCase


class TestFoo(TransactionTestCase):
    def test_foo(self):
        pass
}}}
* `./manage.py test` now fails with:

{{{
Creating test database for alias 'default'…
System check identified no issues (0 silenced).
.E
======================================================================
ERROR: test_foo (foo.tests.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/sqlite3/base.py"", line 328, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py"", line 211, in __call__
    self._post_teardown()
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py"", line 926, in _post_teardown
    self._fixture_teardown()
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py"", line 967, in _fixture_teardown
    connections[db_name]._test_serialized_contents
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/base/creation.py"", line 133, in deserialize_db_from_string
    obj.save()
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/core/serializers/base.py"", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py"", line 763, in save_base
    force_update, using, update_fields,
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py"", line 854, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py"", line 892, in _do_insert
    using=using, raw=raw)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/manager.py"", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/query.py"", line 1185, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/sql/compiler.py"", line 1323, in execute_sql
    cursor.execute(sql, params)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py"", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py"", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/utils.py"", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/sqlite3/base.py"", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

----------------------------------------------------------------------
Ran 1 test in 0.039s

FAILED (errors=1)
Destroying test database for alias 'default'…
}}}

On a commit prior to b3b1d3d45fc066367f4fcacf0b06f72fcd00a9c6 this succeeds as expected:

{{{
Creating test database for alias 'default'…
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.018s

OK
Destroying test database for alias 'default'…
}}}"	Bug	closed	Testing framework	dev	Release blocker	fixed		Romain Garrigues	Accepted	1	0	0	1	0	0
