From 5779cc938a34eb96815c7a40ded2c8f6c8087c58 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 11 Dec 2019 22:34:28 -0500 Subject: [PATCH] Fixed #31071 -- Disabled insert optimization for primary keys with defaults when loading fixtures. Model.save_base() is called directly when loading fixtures and assumes existing rows will be updated. Branching of "raw" allows to maintain the optimization introduced in #29260 while supporting this edge case. Regression in 85458e94e38c20e57939947ee515a1a53689659f. Thanks Reupen Shah for the report. --- django/db/models/base.py | 1 + docs/releases/3.0.2.txt | 4 ++++ tests/serializers/models/data.py | 6 ++++++ tests/serializers/test_data.py | 3 ++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index e3b14a41a0..844c01e95e 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -849,6 +849,7 @@ class Model(metaclass=ModelBase): updated = False # Skip an UPDATE when adding an instance and primary key has a default. if ( + not raw and not force_insert and self._state.adding and self._meta.pk.default and diff --git a/docs/releases/3.0.2.txt b/docs/releases/3.0.2.txt index ebb85d1f46..e232fa81f8 100644 --- a/docs/releases/3.0.2.txt +++ b/docs/releases/3.0.2.txt @@ -18,3 +18,7 @@ Bugfixes * Fixed a regression in Django 3.0 that caused a migration crash on PostgreSQL 10+ when adding a foreign key and changing data in the same migration (:ticket:`31106`). + +* Fixed a regression in Django 3.0 where loading fixtures crashed for models + defining a :attr:`~django.db.models.Field.default` for the primary key + (:ticket:`31071`). diff --git a/tests/serializers/models/data.py b/tests/serializers/models/data.py index 89564f756c..eaa2aa60e6 100644 --- a/tests/serializers/models/data.py +++ b/tests/serializers/models/data.py @@ -4,6 +4,8 @@ The following classes are for testing basic data marshalling, including NULL values, where allowed. The basic idea is to have a model for each Django data type. """ +import uuid + from django.contrib.contenttypes.fields import ( GenericForeignKey, GenericRelation, ) @@ -257,6 +259,10 @@ class UUIDData(models.Model): data = models.UUIDField(primary_key=True) +class UUIDDefaultData(models.Model): + data = models.UUIDField(primary_key=True, default=uuid.uuid4) + + class FKToUUID(models.Model): data = models.ForeignKey(UUIDData, models.CASCADE) diff --git a/tests/serializers/test_data.py b/tests/serializers/test_data.py index 493da7e7cc..1ddba02565 100644 --- a/tests/serializers/test_data.py +++ b/tests/serializers/test_data.py @@ -26,7 +26,7 @@ from .models import ( ModifyingSaveData, NullBooleanData, O2OData, PositiveBigIntegerData, PositiveIntegerData, PositiveIntegerPKData, PositiveSmallIntegerData, PositiveSmallIntegerPKData, SlugData, SlugPKData, SmallData, SmallPKData, - Tag, TextData, TimeData, UniqueAnchor, UUIDData, + Tag, TextData, TimeData, UniqueAnchor, UUIDData, UUIDDefaultData, ) from .tests import register_tests @@ -351,6 +351,7 @@ The end."""), # (pk_obj, 790, XMLPKData, ""), (pk_obj, 791, UUIDData, uuid_obj), (fk_obj, 792, FKToUUID, uuid_obj), + (pk_obj, 793, UUIDDefaultData, uuid_obj), (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)), (data_obj, 810, ModifyingSaveData, 42),