Code

Ticket #12560: 12560.diff

File 12560.diff, 1.8 KB (added by Honza_Kral, 4 years ago)

Quick fix to get the discussion started, may not be 100%

Line 
1diff --git a/django/db/models/base.py b/django/db/models/base.py
2index 06db7cc..dcc1ca2 100644
3--- a/django/db/models/base.py
4+++ b/django/db/models/base.py
5@@ -696,7 +696,7 @@ class Model(object):
6             for field_name in unique_check:
7                 f = self._meta.get_field(field_name)
8                 lookup_value = getattr(self, f.attname)
9-                if f.null and lookup_value is None:
10+                if lookup_value is None:
11                     # no value, skip the lookup
12                     continue
13                 if f.primary_key and not getattr(self, '_adding', False):
14diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
15index cbb56aa..8ae946c 100644
16--- a/tests/modeltests/validation/test_unique.py
17+++ b/tests/modeltests/validation/test_unique.py
18@@ -42,12 +42,20 @@ class PerformUniqueChecksTest(unittest.TestCase):
19         settings.DEBUG = self._old_debug
20         super(PerformUniqueChecksTest, self).tearDown()
21 
22-    def test_primary_key_unique_check_performed_when_adding(self):
23+    def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
24         """Regression test for #12132"""
25         l = len(connection.queries)
26         mtv = ModelToValidate(number=10, name='Some Name')
27         setattr(mtv, '_adding', True)
28         mtv.full_validate()
29+        self.assertEqual(l, len(connection.queries))
30+
31+    def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
32+        """Regression test for #12132"""
33+        l = len(connection.queries)
34+        mtv = ModelToValidate(number=10, name='Some Name', id=123)
35+        setattr(mtv, '_adding', True)
36+        mtv.full_validate()
37         self.assertEqual(l+1, len(connection.queries))
38 
39     def test_primary_key_unique_check_not_performed_when_not_adding(self):