﻿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
31398	multiple_database.tests.AuthTestCase doesn't flush the default database if transactions aren't supported.	Tim Graham	Nick Michas	"On databases that don't support transactions, the database is flushed between test methods.

In Django's test suite, [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/tests.py#L1527-L1577 multiple_database.tests.AuthTestCase.test_auth_manager()] is the first test to run in that class. It creates two Users: alice in the 'other' database and bob in the 'default' database.

The test class uses `AuthRouter` which has an [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/routers.py#L48-L52 allow_migrate()] method that makes sure the auth app only appears in the 'other' db, however, the test database creation doesn't respect that router and so the table is created in both databases (thus no errors when creating the users as described above).

The problem comes during `TransactionTestCase. _fixture_teardown()` after the first test. It invokes the flush command which doesn't include any tables from the auth app because of the `allow_migrate()` method on the router.  (The flush command calls [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/core/management/commands/flush.py#L47 django.core.management.sql.sql_flush] which calls [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/core/management/sql.py#L13 BaseDatabaseIntrospection.django_table_names()] which calls [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/db/backends/base/introspection.py#L85 router.get_migratable_models()] which eventually invokes [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/db/utils.py#L309-L320 allow_migrate()].) Since the auth_user table isn't flushed after the first test, an `IntegrityError` (violating the unique username constraint) is raised when [https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/tests.py#L1564 the second test creates bob in the 'default' database]."	Bug	assigned	Core (Other)	dev	Normal			Ülgen Sarıkavak	Accepted	1	0	0	0	0	0
