Re: [Django] #31646: Support annotated check constraints.

5 views
Skip to first unread message

Django

unread,
May 30, 2020, 7:27:25 PM5/30/20
to django-...@googlegroups.com
#31646: Support annotated check constraints.
-------------------------------------+-------------------------------------
Reporter: Tonye Jack | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: check-constraints, | Triage Stage:
model constraints | Unreviewed
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tonye Jack):

* needs_docs: 0 => 1
* needs_tests: 0 => 1


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

Django

unread,
May 31, 2020, 7:15:49 AM5/31/20
to django-...@googlegroups.com
#31646: Support annotated check constraints.
-------------------------------------+-------------------------------------
Reporter: Tonye Jack | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: check-constraints, | Triage Stage:
model constraints | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: new => closed
* needs_docs: 1 => 0
* resolution: => wontfix
* needs_tests: 1 => 0
* easy: 1 => 0


Comment:

`CheckConstraint` accepts any boolean expression since Django 3.1+ so this
particular one can be expressed using `RawSQL`

{{{#!python
CheckConstraint(
check=RawSQL(
'non_null_count(amount::integer , amount_off::integer,
percentage::integer) = 1',
output_field=models.BooleanField(),
)
)
}}}

Or event `Func`, `Cast`, and `Exact`.

{{{#!python
non_null_count = Func(Cast('amount', models.IntegerField()),
Cast('amount_off', models.IntegerField()), Cast('percentage',
models.IntegerField()), function='non_null_count')
CheckConstraint(
check=Exact(non_null_count, 1),
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31646#comment:2>

Reply all
Reply to author
Forward
0 new messages