Skip to content

Commit

Permalink
Fixed #35425 -- Avoided INSERT with force_update and explicit pk.
Browse files Browse the repository at this point in the history
Affected models where the primary key field is defined with a
default or db_default, such as UUIDField.
  • Loading branch information
jacobtylerwalls committed May 4, 2024
1 parent 9a27c76 commit f571f19
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ def _save_table(
if (
not raw
and not force_insert
and not force_update
and self._state.adding
and (
(meta.pk.default and meta.pk.default is not NOT_PROVIDED)
Expand Down
6 changes: 6 additions & 0 deletions tests/basic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def test_save_primary_with_default(self):
with self.assertNumQueries(1):
PrimaryKeyWithDefault().save()

def test_save_primary_with_default_force_update(self):
# An UPDATE attempt is made if explicitly requested.
obj = PrimaryKeyWithDefault.objects.create()
with self.assertNumQueries(1):
PrimaryKeyWithDefault(uuid=obj.pk).save(force_update=True)

def test_save_primary_with_db_default(self):
# An UPDATE attempt is skipped when a primary key has db_default.
with self.assertNumQueries(1):
Expand Down

0 comments on commit f571f19

Please sign in to comment.