Ticket #15130: 15130.2.diff

File 15130.2.diff, 4.5 KB (added by Ramiro Morales, 14 years ago)

New version of the patch with fixes suggested by Alex

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    a b  
    717717            if len(unique_check) != len(lookup_kwargs.keys()):
    718718                continue
    719719
    720             qs = model_class._default_manager.filter(**lookup_kwargs)
     720            using = router.db_for_write(self.__class__, instance=self)
     721            qs = model_class._default_manager.using(using).filter(**lookup_kwargs)
    721722
    722723            # Exclude the current object from the query if we are editing an
    723724            # instance (as opposed to creating a new one)
     
    750751                lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
    751752            lookup_kwargs[field] = getattr(self, field)
    752753
    753             qs = model_class._default_manager.filter(**lookup_kwargs)
     754            using = router.db_for_write(self.__class__, instance=self)
     755            qs = model_class._default_manager.using(using).filter(**lookup_kwargs)
    754756            # Exclude the current object from the query if we are editing an
    755757            # instance (as opposed to creating a new one)
    756758            if not self._state.adding and self.pk is not None:
  • tests/modeltests/validation/test_unique.py

    diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
    a b  
    149149            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
    150150        except:
    151151            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
     152
     153class MultiDbUniqueTests(TestCase):
     154    multi_db = True
     155
     156    def test_unique_checks_multi_db_isolation_unique(self):
     157        UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3).save(using='default')
     158        m = UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3)
     159        m._state.db = 'other'
     160        try:
     161            m.full_clean()
     162        except ValidationError:
     163            self.fail("unique field validation shouldn't erroneosuly trigger when the identical model instances are on different databases.")
     164        else:
     165            m.save()
     166            self.assertEqual(UniqueFieldsModel.objects.using('default').count(), 1)
     167            self.assertEqual(UniqueFieldsModel.objects.using('other').count(), 1)
     168
     169    def test_unique_checks_multi_db_isolation_unique_together(self):
     170        UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org').save(using='default')
     171        m = UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org')
     172        m._state.db = 'other'
     173        try:
     174            m.full_clean()
     175        except ValidationError:
     176            self.fail("Meta.unique_together validation shouldn't erroneosuly trigger when the identical model instances are on different databases.")
     177        else:
     178            m.save()
     179            self.assertEqual(UniqueTogetherModel.objects.using('default').count(), 1)
     180            self.assertEqual(UniqueTogetherModel.objects.using('other').count(), 1)
     181
     182    def test_unique_checks_multi_db_isolation_unique_for_x(self):
     183        today = datetime.date.today()
     184        now = datetime.datetime.now()
     185        UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo').save(using='default')
     186        m = UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo')
     187        m._state.db = 'other'
     188        try:
     189            m.full_clean()
     190        except ValidationError:
     191            self.fail("Meta.unique_for_* validation shouldn't erroneosuly trigger when the identical model instances are on different databases.")
     192        else:
     193            m.save()
     194            self.assertEqual(UniqueForDateModel.objects.using('default').count(), 1)
     195            self.assertEqual(UniqueForDateModel.objects.using('other').count(), 1)
  • tests/modeltests/validation/tests.py

    diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
    a b  
    77# Import other tests for this package.
    88from modeltests.validation.validators import TestModelsWithValidators
    99from modeltests.validation.test_unique import (GetUniqueCheckTests,
    10     PerformUniqueChecksTest)
     10    PerformUniqueChecksTest, MultiDbUniqueTests)
    1111from modeltests.validation.test_custom_messages import CustomMessagesTest
    1212
    1313
Back to Top