Code

Ticket #15304: 15304-pk-type-r15539.diff

File 15304-pk-type-r15539.diff, 1.9 KB (added by mrmachine, 3 years ago)
Line 
1Index: docs/intro/tutorial01.txt
2===================================================================
3--- docs/intro/tutorial01.txt   (revision 15539)
4+++ docs/intro/tutorial01.txt   (working copy)
5@@ -534,10 +534,7 @@
6     # Save the object into the database. You have to call save() explicitly.
7     >>> p.save()
8 
9-    # Now it has an ID. Note that this might say "1L" instead of "1", depending
10-    # on which database you're using. That's no biggie; it just means your
11-    # database backend prefers to return integers as Python long integer
12-    # objects.
13+    # Now it has an ID.
14     >>> p.id
15     1
16 
17Index: tests/regressiontests/model_regress/tests.py
18===================================================================
19--- tests/regressiontests/model_regress/tests.py        (revision 15539)
20+++ tests/regressiontests/model_regress/tests.py        (working copy)
21@@ -163,6 +163,13 @@
22             1
23         )
24 
25+    def test_pk_type(self):
26+        # Make sure that the primary key value of newly created objects is the
27+        # same type as existing objects.
28+        p = Party.objects.create()
29+        p2 = Party.objects.get(pk=p.pk)
30+        self.assertEqual(type(p.pk), type(p2.pk))
31+
32 class ModelValidationTest(TestCase):
33     def test_pk_validation(self):
34         one = NonAutoPK.objects.create(name="one")
35Index: django/db/models/base.py
36===================================================================
37--- django/db/models/base.py    (revision 15539)
38+++ django/db/models/base.py    (working copy)
39@@ -556,7 +556,7 @@
40                     result = manager._insert([(meta.pk, connection.ops.pk_default_value())], return_id=update_pk, raw_values=True, using=using)
41 
42                 if update_pk:
43-                    setattr(self, meta.pk.attname, result)
44+                    setattr(self, meta.pk.attname, meta.auto_field.to_python(result))
45             transaction.commit_unless_managed(using=using)
46 
47         # Store the database on which the object was saved