#24509: Allow Expressions when saving new models
-------------------------------------+-------------------------------------
Reporter: AlexHill | Owner:
Type: New feature | Status: new
Component: Database layer | Version: master
(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 AlexHill):
* has_patch: 0 => 1
Old description:
> Opening a ticket after discussion on IRC with Josh.
>
> You can use `Expression` when updating models, but it would also be
> useful to be able to use them when creating models, particularly in order
> to apply database functions to newly-inserted values.
>
> My specific use case for this is a PostgreSQL `TSVectorField`, where
> instead of just inserting a value, I want to insert
> `strip(to_tsvector(<ts_config>, unaccent(<value>)))`. Ideally, I would be
> able to define these all as `Func`s and insert an `Expression`.
>
> I propose to fix this by making `SQLInsertCompiler` check for
> `resolve_expressions` and `as_sql`, as `SQLUpdateCompiler` does.
>
> Alex
New description:
Opening a ticket after discussion on IRC with Josh.
You can use `Expression` when updating models, but it would also be useful
to be able to use them when creating models, particularly in order to
apply database functions to newly-inserted values.
My specific use case for this is a PostgreSQL `TSVectorField`, where
instead of just inserting a value, I want to insert
`strip(to_tsvector(<ts_config>, unaccent(<value>)))`. Ideally, I would be
able to define these all as `Func`s and insert an `Expression`.
I propose to fix this by making `SQLInsertCompiler` check for
`resolve_expressions` and `as_sql`, as `SQLUpdateCompiler` does.
Alex
Update: patch at https://github.com/django/django/pull/5099
--
Comment:
Here's my patch implementing this:
https://github.com/django/django/pull/5099
It also adds support for expressions (and placeholders) in bulk inserts.
Notes:
* As Carl suggested, this should allow us to get rid of
`get_placeholder()` entirely.
* The handling of `AutoField`s is a bit ugly at the moment. If an
`AutoField`'s value is `None`, it's removed from the list of fields in
`Model._save_table()` and then conditionally re-added in
`SQLInsertCompiler.as_sql()`. I'm thinking about nicer ways to handle
this.
* There is a heap of same-but-different code between `SQLInsertCompiler`
and `SQLUpdateCompiler`, especially now that expressions are supported.
I'd like to clean this up in a subsequent patch.
* I've changed the workaround for #10888. I'm pretty sure it won't change
anything, but I can't test in Oracle.
--
Ticket URL: <https://code.djangoproject.com/ticket/24509#comment:7>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.11b16da8f76971606deb5d6d0a023ddb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.