#36133: ExpressionWrapper output_field no longer works
-------------------------------------+-------------------------------------
Reporter: Siburg | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: Cast, | Triage Stage:
ExpressionWrapper | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* resolution: => invalid
* status: new => closed
Comment:
The issue you are running into has more to do with `Concat` changes than
`ExpressionWrapper`
The pre-5.1 SQL was along the lines of
{{{#!sql
CONCAT(('zzzzzzzz')::text, ((999999 - "test_34444_partner"."id"))::text)
}}}
and the post 5.1 SQL is
{{{#!sql
COALESCE('zzzzzzzz', '') || COALESCE((999999 - "test_34444_partner"."id"),
'')
}}}
due to the change to string concatenation on Postgres to the immutable
`||` in #34955.
The reason why your code broke is that you explicitly tell the ORM that
`ExpressionWrapper(999_999 - F('pk'), output_field=models.CharField())`
resolves to `CharField` when it actually resolves to `BigIntegerField`
which prevents the ORM from
[https://github.com/django/django/blob/352d860b9107adbcde0f1fe5d0fce8e9090a51e4/django/db/models/functions/text.py#L92-L96
implicitly applying adequate casting].
Simply removing all usage of `ExpressionWrapper` should solve your issue
as it's doing more harm than good in its current form.
{{{#!python
Concat(
Value("zzzzzzzz"),
999_999 - F("pk"),
output_field=models.CharField(),
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36133#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070194957e8b50-f93963df-13cc-496e-92a4-a96c23f1bbe2-000000%40eu-central-1.amazonses.com.