Skip to content

Commit

Permalink
[3.0.x] Fixed #31071 -- Disabled insert optimization for primary keys…
Browse files Browse the repository at this point in the history
… 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 85458e9.

Thanks Reupen Shah for the report.

Backport of 5779cc9 from master
  • Loading branch information
charettes authored and felixxm committed Dec 30, 2019
1 parent 0f8041a commit 7db4ab8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ def _save_table(self, raw=False, cls=None, force_insert=False,
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
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/3.0.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
6 changes: 6 additions & 0 deletions tests/serializers/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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,
)
Expand Down Expand Up @@ -253,6 +255,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)

Expand Down
3 changes: 2 additions & 1 deletion tests/serializers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ModifyingSaveData, NullBooleanData, O2OData, PositiveIntegerData,
PositiveIntegerPKData, PositiveSmallIntegerData,
PositiveSmallIntegerPKData, SlugData, SlugPKData, SmallData, SmallPKData,
Tag, TextData, TimeData, UniqueAnchor, UUIDData,
Tag, TextData, TimeData, UniqueAnchor, UUIDData, UUIDDefaultData,
)
from .tests import register_tests

Expand Down Expand Up @@ -349,6 +349,7 @@ def inherited_compare(testcase, pk, klass, data):
# (pk_obj, 790, XMLPKData, "<foo></foo>"),
(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),
Expand Down

0 comments on commit 7db4ab8

Please sign in to comment.