1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

Fixed #36064 -- Skipped an UPDATE when adding a model instance with a composite primary key with default values.

This commit is contained in:
Bendeguz Csirmaz
2025-01-10 08:47:10 +01:00
committed by Sarah Boyce
parent 8287fd4915
commit 5851605863
3 changed files with 17 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
from django.db import IntegrityError
from django.test import TestCase, skipUnlessDBFeature
from .models import Tenant, User
from .models import Post, Tenant, User
class CompositePKCreateTests(TestCase):
@@ -8,7 +9,7 @@ class CompositePKCreateTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.tenant = Tenant.objects.create()
cls.tenant = Tenant.objects.create(id=1)
cls.user = User.objects.create(
tenant=cls.tenant,
id=1,
@@ -151,3 +152,12 @@ class CompositePKCreateTests(TestCase):
self.assertEqual(user.email, fields["defaults"]["email"])
self.assertEqual(user.email, f"user{user.id}@example.com")
self.assertEqual(count + 1, User.objects.count())
def test_save_default_pk_not_set(self):
with self.assertNumQueries(1):
Post().save()
def test_save_default_pk_set(self):
post = Post.objects.create()
with self.assertRaises(IntegrityError):
Post(tenant_id=post.tenant_id, id=post.id).save()