Re: [Django] #34985: Migrations raise AppRegistryNotReady when GeneratedField references incorrect fields.

19 views
Skip to first unread message

Django

unread,
Nov 22, 2023, 12:07:29 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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

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

Or we can move resolving an expression to the `generated_sql()` which is
not the end of the world:
{{{#!diff
diff --git a/django/db/models/fields/generated.py
b/django/db/models/fields/generated.py
index 9a73b7fe37..95d19582de 100644
--- a/django/db/models/fields/generated.py
+++ b/django/db/models/fields/generated.py
@@ -13,7 +13,6 @@ class GeneratedField(Field):
db_returning = True

_query = None
- _resolved_expression = None
output_field = None

def __init__(self, *, expression, output_field, db_persist=None,
**kwargs):
@@ -48,9 +47,6 @@ class GeneratedField(Field):
super().contribute_to_class(*args, **kwargs)

self._query = Query(model=self.model, alias_cols=False)
- self._resolved_expression = self.expression.resolve_expression(
- self._query, allow_joins=False
- )
# Register lookups from the output_field class.
for lookup_name, lookup in
self.output_field.get_class_lookups().items():
self.register_lookup(lookup, lookup_name=lookup_name)
@@ -59,7 +55,10 @@ class GeneratedField(Field):
compiler = connection.ops.compiler("SQLCompiler")(
self._query, connection=connection, using=None
)
- return compiler.compile(self._resolved_expression)
+ resolved_expression = self.expression.resolve_expression(
+ self._query, allow_joins=False
+ )
+ return compiler.compile(resolved_expression)

def check(self, **kwargs):
databases = kwargs.get("databases") or []

}}}

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

Django

unread,
Nov 22, 2023, 2:11:13 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned

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):

* owner: nobody => Mariusz Felisiak
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:3>

Django

unread,
Nov 22, 2023, 2:56:52 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

On reconsideration, system check is not necessary. `makemigrations`
doesn't crash with [https://code.djangoproject.com/ticket/34985#comment:2
the proposed diff], and `migrate` raises:
{{{
django.core.exceptions.FieldError: Joined field references are not
permitted in this query
}}}
which is fine, IMO.

--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:4>

Django

unread,
Nov 22, 2023, 4:50:11 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: field, database, | Triage Stage: Accepted
generated, output_field |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/17506 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:5>

Django

unread,
Nov 22, 2023, 6:30:05 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: field, database, | Triage Stage: Ready for
generated, output_field | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:6>

Django

unread,
Nov 22, 2023, 7:41:44 AM11/22/23
to django-...@googlegroups.com
#34985: Migrations raise AppRegistryNotReady when GeneratedField references
incorrect fields.
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed

Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: field, database, | Triage Stage: Accepted
generated, output_field |

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"101a85a5a06585ba16ecb25860146d034a8a55ec" 101a85a]:
{{{
#!CommitTicketReference repository=""
revision="101a85a5a06585ba16ecb25860146d034a8a55ec"
Fixed #34985 -- Fixed GeneratedFields.contribute_to_class() crash when
apps are not populated.

Thanks Paolo Melchiorre for the report.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.

Django

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

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"48eebdc63caca18fef90ce49dd5473fd338ebe94" 48eebdc]:
{{{
#!CommitTicketReference repository=""
revision="48eebdc63caca18fef90ce49dd5473fd338ebe94"
[5.0.x] Fixed #34985 -- Fixed GeneratedFields.contribute_to_class() crash


when apps are not populated.

Thanks Paolo Melchiorre for the report.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.
Backport of 101a85a5a06585ba16ecb25860146d034a8a55ec from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34985#comment:7>

Reply all
Reply to author
Forward
0 new messages