Re: [Django] #34985: Migrations raise AppRegistryNotReady when GeneratedField references incorrect fields. (was: Confusing migration failures in GeneratedField)

1 view
Skip to first unread message

Django

unread,
Nov 22, 2023, 12:00:37 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: field, database, | Triage Stage: Accepted
generated, output_field |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Lily Foote (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. We could add a small check for `GeneratedField`
references, e.g.
{{{#!diff
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 64d54380da..62ce34c4f8 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -1601,6 +1601,7 @@ class Model(AltersData, metaclass=ModelBase):
*cls._check_constraints(databases),
*cls._check_default_pk(),
*cls._check_db_table_comment(databases),
+ *cls._check_generated_fields(databases),
]

return errors
@@ -1957,6 +1958,18 @@ class Model(AltersData, metaclass=ModelBase):
errors.extend(cls._check_local_fields(fields,
"unique_together"))
return errors

+ @classmethod
+ def _check_generated_fields(cls, databases):
+ errors = []
+ for f in cls._meta.local_fields:
+ if not f.generated:
+ continue
+ references = {
+ ref[0] for ref in cls._get_expr_references(f.expression)
+ }
+ errors.extend(cls._check_local_fields(references, f.name))
+ return errors
+
@classmethod
def _check_indexes(cls, databases):
"""Check fields, names, and conditions of indexes."""

}}}

Unfortunately, this would still required catching exceptions when
resolving `GeneratedField` expressions.

--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages