[Django] #37239: After assigning a related object with a db_default, the db_default expression is reevaluated when saving the other object

4 views
Skip to first unread message

Django

unread,
Jul 28, 2026, 11:54:02 PM (15 hours ago) Jul 28
to django-...@googlegroups.com
#37239: After assigning a related object with a db_default, the db_default
expression is reevaluated when saving the other object
-------------------------------------+-------------------------------------
Reporter: Jacob | Owner: Jacob Walls
Walls |
Type: Bug | Status: assigned
Component: Database | Version: 5.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
With these models:
{{{#!py
class DBDefaultsFunctionPK(models.Model):
uuid = models.UUIDField(primary_key=True, db_default=UUID4())

class Meta:
required_db_features = {
"supports_uuid4_function",
"supports_expression_defaults",
}


class DBDefaultsFunctionPKChild(DBDefaultsFunctionPK):
parent = models.OneToOneField(
DBDefaultsFunctionPK,
models.CASCADE,
primary_key=True,
db_default=UUID4(),
related_name="child",
)

class Meta:
required_db_features = {
"supports_uuid4_function",
"supports_expression_defaults",
}
}}}
This test fails:
{{{#!py
@skipUnlessDBFeature(
"can_return_rows_from_bulk_insert",
"supports_expression_defaults",
"supports_uuid4_function",
)
def test_foreign_key_db_default_expression_via_parent(self):
parent = DBDefaultsFunctionPK()
obj = DBDefaultsFunctionPKChild(parent=parent)
parent.save()
obj.save()
self.assertEqual(obj.pk, parent.pk)
}}}
{{{
======================================================================
FAIL: test_foreign_key_db_default_expression_via_parent
(field_defaults.tests.DefaultTests.test_foreign_key_db_default_expression_via_parent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jwalls/django/tests/field_defaults/tests.py", line 152, in
test_foreign_key_db_default_expression_via_parent
self.assertEqual(obj.pk, parent.pk)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: UUID('fdfe6046-e662-4c36-a2e1-b10677d0ccb3') != UUID
('ddeef3dd-52bc-4b35-b643-4634876a4acc')

----------------------------------------------------------------------
Ran 1 test in 0.016s

FAILED (failures=1, errors=1)
}}}
----
A similar version of the test using `Now()` instead of `UUID4()` fails on
5.2 (since `UUID4()` is not available there), but I ran into issues with
trying to use datetimes in FK constraints on sqlite, so I'm happy to take
advice on the best examples to build these models with, as I could be
glazing over something obviously better.
----
This is the "separate non-release-blocker ticket" mentioned in #37238, and
the underlying reason for the relaxed assertions on the PR for it.
--
Ticket URL: <https://code.djangoproject.com/ticket/37239>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 28, 2026, 11:58:08 PM (15 hours ago) Jul 28
to django-...@googlegroups.com
#37239: After assigning a related object with a db_default, the db_default
expression is reevaluated when saving the other object
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/21694 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37239#comment:1>

Django

unread,
3:48 AM (11 hours ago) 3:48 AM
to django-...@googlegroups.com
#37239: After assigning a related object with a db_default, the db_default
expression is reevaluated when saving the other object
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* cc: Lily Foote (added)
* stage: Unreviewed => Accepted

Comment:

Thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/37239#comment:2>

Django

unread,
4:23 AM (11 hours ago) 4:23 AM
to django-...@googlegroups.com
#37239: After assigning a related object with a db_default, the db_default
expression is reevaluated when saving the other object
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37239#comment:3>
Reply all
Reply to author
Forward
0 new messages