Fixed #12560. Changed validate_unique to stop checking null primary key values. Thanks, Honza Král.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12227 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Joseph Kocherhans 2010-01-14 17:04:53 +00:00
parent 2b2db12032
commit a2a5602b19
3 changed files with 17 additions and 9 deletions

View File

@ -256,7 +256,7 @@ answer newbie questions, and generally made Django that much better:
Gasper Koren Gasper Koren
Martin Kosír <martin@martinkosir.net> Martin Kosír <martin@martinkosir.net>
Arthur Koziel <http://arthurkoziel.com> Arthur Koziel <http://arthurkoziel.com>
Honza Kral <honza.kral@gmail.com> Honza Král <honza.kral@gmail.com>
Meir Kriheli <http://mksoft.co.il/> Meir Kriheli <http://mksoft.co.il/>
Bruce Kroeze <http://coderseye.com/> Bruce Kroeze <http://coderseye.com/>
krzysiek.pawlik@silvermedia.pl krzysiek.pawlik@silvermedia.pl

View File

@ -715,7 +715,7 @@ class Model(object):
for field_name in unique_check: for field_name in unique_check:
f = self._meta.get_field(field_name) f = self._meta.get_field(field_name)
lookup_value = getattr(self, f.attname) lookup_value = getattr(self, f.attname)
if f.null and lookup_value is None: if lookup_value is None:
# no value, skip the lookup # no value, skip the lookup
continue continue
if f.primary_key and not getattr(self, '_adding', False): if f.primary_key and not getattr(self, '_adding', False):

View File

@ -43,18 +43,26 @@ class PerformUniqueChecksTest(unittest.TestCase):
settings.DEBUG = self._old_debug settings.DEBUG = self._old_debug
super(PerformUniqueChecksTest, self).tearDown() super(PerformUniqueChecksTest, self).tearDown()
def test_primary_key_unique_check_performed_when_adding(self): def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
"""Regression test for #12132""" # Regression test for #12560
l = len(connection.queries) query_count = len(connection.queries)
mtv = ModelToValidate(number=10, name='Some Name') mtv = ModelToValidate(number=10, name='Some Name')
setattr(mtv, '_adding', True) setattr(mtv, '_adding', True)
mtv.full_clean() mtv.full_clean()
self.assertEqual(l+1, len(connection.queries)) self.assertEqual(query_count, len(connection.queries))
def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
# Regression test for #12560
query_count = len(connection.queries)
mtv = ModelToValidate(number=10, name='Some Name', id=123)
setattr(mtv, '_adding', True)
mtv.full_clean()
self.assertEqual(query_count + 1, len(connection.queries))
def test_primary_key_unique_check_not_performed_when_not_adding(self): def test_primary_key_unique_check_not_performed_when_not_adding(self):
"""Regression test for #12132""" # Regression test for #12132
l = len(connection.queries) query_count= len(connection.queries)
mtv = ModelToValidate(number=10, name='Some Name') mtv = ModelToValidate(number=10, name='Some Name')
mtv.full_clean() mtv.full_clean()
self.assertEqual(l, len(connection.queries)) self.assertEqual(query_count, len(connection.queries))