#30484: Feature request: Using RawSQL with CheckConstraint
-------------------------------------+-------------------------------------
Reporter: David | Owner: nobody
Sanders |
Type: New | Status: new
feature |
Component: Database | Version: 2.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 |
-------------------------------------+-------------------------------------
I'm wondering about the possibility of updating CheckConstraint to handle
RawSQL expressions, in a similar way to annotate().
To illustrate the limitations of using Q objects and usefulness of RawSQL
expressions consider the following example:
{{{
class Foo(models.Model):
…
total = models.IntegerField(blank=True, null=True) # a calculated
field
total_last_updated = models.DateTimeField(blank=True, null=True) #
set when total is calculated
}}}
A check constraint would be useful here to enforce that
`total_last_updated` is populated/cleared when total is populated/cleared
and would be something like an XOR check with the following SQL
expression: `(total IS NULL) = (total_last_updated IS NULL)`. This
doesn't appear to be possible with Q objects.
The workaround in this case is to create the check constraint manually
with a RunSQL migration.
--
Ticket URL: <https://code.djangoproject.com/ticket/30484>
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/051.894f84b4927e6b37c94e0e63271bbfe2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.