Ticket #15130: 15130.1.diff

File 15130.1.diff, 4.4 KB (added by Ramiro Morales, 13 years ago)

Patch fifing this issue by t2y plus fixed tests

  • 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            qs = model_class._default_manager.using(self._state.db).filter(**lookup_kwargs)
    721721
    722722            # Exclude the current object from the query if we are editing an
    723723            # instance (as opposed to creating a new one)
     
    750750                lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
    751751            lookup_kwargs[field] = getattr(self, field)
    752752
    753             qs = model_class._default_manager.filter(**lookup_kwargs)
     753            qs = model_class._default_manager.using(self._state.db).filter(**lookup_kwargs)
    754754            # Exclude the current object from the query if we are editing an
    755755            # instance (as opposed to creating a new one)
    756756            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        except:
     165            raise
     166        else:
     167            m.save()
     168            self.assertEqual(UniqueFieldsModel.objects.using('default').count(), 1)
     169            self.assertEqual(UniqueFieldsModel.objects.using('other').count(), 1)
     170
     171    def test_unique_checks_multi_db_isolation_unique_together(self):
     172        UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org').save(using='default')
     173        m = UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org')
     174        m._state.db = 'other'
     175        try:
     176            m.full_clean()
     177        except ValidationError:
     178            self.fail("Meta.unique_together validation shouldn't erroneosuly trigger when the identical model instances are on different databases.")
     179        except:
     180            raise
     181        else:
     182            m.save()
     183            self.assertEqual(UniqueTogetherModel.objects.using('default').count(), 1)
     184            self.assertEqual(UniqueTogetherModel.objects.using('other').count(), 1)
     185
     186    def test_unique_checks_multi_db_isolation_unique_for_x(self):
     187        today = datetime.date.today()
     188        now = datetime.datetime.now()
     189        UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo').save(using='default')
     190        m = UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo')
     191        m._state.db = 'other'
     192        try:
     193            m.full_clean()
     194        except ValidationError:
     195            self.fail("Meta.unique_for_* validation shouldn't erroneosuly trigger when the identical model instances are on different databases.")
     196        except:
     197            raise
     198        else:
     199            m.save()
     200            self.assertEqual(UniqueForDateModel.objects.using('default').count(), 1)
     201            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