diff --git a/django/contrib/sites/management.py b/django/contrib/sites/management.py index 99ec79395f..b3a8347c14 100644 --- a/django/contrib/sites/management.py +++ b/django/contrib/sites/management.py @@ -17,7 +17,7 @@ def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT if not router.allow_migrate(using, Site): return - if not Site.objects.exists(): + if not Site.objects.using(using).exists(): # The default settings set SITE_ID = 1, and some tests in Django's test # suite rely on this value. However, if database sequences are reused # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py index 0e704763eb..e9f68c8727 100644 --- a/django/contrib/sites/tests.py +++ b/django/contrib/sites/tests.py @@ -107,7 +107,7 @@ class CreateDefaultSiteTests(TestCase): self.assertEqual(Site.objects.count(), 1) @unittest.skipIf('other' not in connections, "Requires 'other' database connection.") - def test_multi_db(self): + def test_multi_db_with_router(self): """ #16353, #16828 - The default site creation should respect db routing. """ @@ -121,6 +121,13 @@ class CreateDefaultSiteTests(TestCase): finally: router.routers = old_routers + @unittest.skipIf('other' not in connections, "Requires 'other' database connection.") + def test_multi_db(self): + create_default_site(self.app_config, using='default', verbosity=0) + create_default_site(self.app_config, using='other', verbosity=0) + self.assertTrue(Site.objects.using('default').exists()) + self.assertTrue(Site.objects.using('other').exists()) + def test_save_another(self): """ #17415 - Another site can be created right after the default one. diff --git a/docs/releases/1.7.5.txt b/docs/releases/1.7.5.txt index afce510aae..604cf9146a 100644 --- a/docs/releases/1.7.5.txt +++ b/docs/releases/1.7.5.txt @@ -16,3 +16,6 @@ Bugfixes * Fixed a regression that prevented custom fields inheriting from ``ManyToManyField`` from being recognized in migrations (:ticket:`24236`). + +* Fixed crash in ``contrib.sites`` migrations when a default database isn't + used (:ticket:`24332`).