Fixed #29260 -- Skipped an UPDATE when adding a model instance with primary key that has a default.

This commit is contained in:
Hasan Ramezani 2019-08-17 15:30:29 +02:00 committed by Mariusz Felisiak
parent e9f74f53cc
commit 85458e94e3
3 changed files with 24 additions and 1 deletions

View file

@ -3,6 +3,8 @@ Bare-bones model
This is a basic model with only two non-primary-key fields.
"""
import uuid
from django.db import models
@ -40,3 +42,7 @@ class SelfRef(models.Model):
# This method intentionally doesn't work for all cases - part
# of the test for ticket #20278
return SelfRef.objects.get(selfref=self).pk
class PrimaryKeyWithDefault(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)

View file

@ -10,7 +10,10 @@ from django.test import (
)
from django.utils.translation import gettext_lazy
from .models import Article, ArticleSelectOnSave, FeaturedArticle, SelfRef
from .models import (
Article, ArticleSelectOnSave, FeaturedArticle, PrimaryKeyWithDefault,
SelfRef,
)
class ModelInstanceCreationTests(TestCase):
@ -130,6 +133,11 @@ class ModelInstanceCreationTests(TestCase):
# ... but there will often be more efficient ways if that is all you need:
self.assertTrue(Article.objects.filter(id=a.id).exists())
def test_save_primary_with_default(self):
# An UPDATE attempt is skipped when a primary key has default.
with self.assertNumQueries(1):
PrimaryKeyWithDefault().save()
class ModelTest(TestCase):
def test_objects_attribute_is_only_available_on_the_class_itself(self):