mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #29260 -- Skipped an UPDATE when adding a model instance with primary key that has a default.
This commit is contained in:
committed by
Mariusz Felisiak
parent
e9f74f53cc
commit
85458e94e3
@@ -15,6 +15,7 @@ from django.db import (
|
||||
DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, DatabaseError, connection,
|
||||
connections, router, transaction,
|
||||
)
|
||||
from django.db.models import NOT_PROVIDED
|
||||
from django.db.models.constants import LOOKUP_SEP
|
||||
from django.db.models.constraints import CheckConstraint, UniqueConstraint
|
||||
from django.db.models.deletion import CASCADE, Collector
|
||||
@@ -842,6 +843,14 @@ class Model(metaclass=ModelBase):
|
||||
if not pk_set and (force_update or update_fields):
|
||||
raise ValueError("Cannot force an update in save() with no primary key.")
|
||||
updated = False
|
||||
# Skip an UPDATE when adding an instance and primary key has a default.
|
||||
if (
|
||||
not force_insert and
|
||||
self._state.adding and
|
||||
self._meta.pk.default and
|
||||
self._meta.pk.default is not NOT_PROVIDED
|
||||
):
|
||||
force_insert = True
|
||||
# If possible, try an UPDATE. If that doesn't update anything, do an INSERT.
|
||||
if pk_set and not force_insert:
|
||||
base_qs = cls._base_manager.using(using)
|
||||
|
||||
Reference in New Issue
Block a user