#15130: Model.validate_unique method doesn't take in account multi-db -------------------------------------------+-------------------------------- Reporter: t2y | Owner: Status: reopened | Milestone: 1.3 Component: ORM aggregation | Version: 1.2 Resolution: | Keywords: multi-db Triage Stage: Accepted | Has patch: 1 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 1 | -------------------------------------------+-------------------------------- Changes (by ramiro):
* needs_better_patch: 0 => 1 Comment: Russell suggested to avoid modifying `._state.db` so test were changed so the fist model is saved to the 'other' DB and the second one to the 'default' DB: {{{ #!python class MultiDbUniqueTests(TestCase): multi_db = True def test_unique_multi_db_isolation(self): "Attempt to save two model instances that don't pass Field.unique checks to different DBs." UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3).save(using='other') b = UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3) try: b.full_clean() except ValidationError: self.fail("unique field validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") else: b.save() self.assertEqual(UniqueFieldsModel.objects.using('default').count(), 1) self.assertEqual(UniqueFieldsModel.objects.using('other').count(), 1) def test_unique_together_multi_db_isolation(self): "Attempt to save two model instances that don't pass Meta.unique_together checks to different DBs." UniqueTogetherModel(cfield='Hello world', ifield=42, efield='u...@example.org').save(using='other') b = UniqueTogetherModel(cfield='Hello world', ifield=42, efield='u...@example.org') try: b.full_clean() except ValidationError: self.fail("Meta.unique_together validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") else: b.save() self.assertEqual(UniqueTogetherModel.objects.using('default').count(), 1) self.assertEqual(UniqueTogetherModel.objects.using('other').count(), 1) def test_unique_for_x_multi_db_isolation(self): "Attempt to save two model instances that don't pass Field.unique_for checks to different DBs." today = datetime.date.today() now = datetime.datetime.now() UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo').save(using='other') b = UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo') try: b.full_clean() except ValidationError: self.fail("Meta.unique_for_* validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") else: b.save() self.assertEqual(UniqueForDateModel.objects.using('default').count(), 1) self.assertEqual(UniqueForDateModel.objects.using('other').count(), 1) }}} Problem is: In this case these tests don't fail if the changes to `django/db/models/base.py` are undone. So it seems there is some deeper problem here. -- Ticket URL: <http://code.djangoproject.com/ticket/15130#comment:11> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.