#33192: It is not possible to use a custom lookup/transorm in a CheckConstraint
-------------------------------------+-------------------------------------
Reporter: Fabien MICHEL | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: lookup, transform, | Triage Stage:
CheckContraint, migrate | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Fabien MICHEL:
Old description:
> Using a custom lookup or transform in a **CheckContraint** make migrate
> command fail.
>
> {{{
>
> class MonthLastDay(Transform):
> lookup_name = "month_last_day"
> output_field = DateField()
> template = "(DATE_TRUNC('month', %(expressions)s) + (interval '1
> month - 1 day'))::date"
>
> DateField.register_lookup(MonthLastDay)
>
> class MyModel(models.Model):
> class Meta:
> constraints = [
> CheckConstraint(check=Q(date__month_last_day__gte=F("period_start")),
> name="date_month_end_after_period_start"),
> ]
>
> date = models.DateField()
> period_start = models.DateField()
> }}}
>
> django **makemigrations command** does not indicate any issue with this
> and make use of `month_last_day` lookup as if it know about it.
>
> Migration command for this contraint:
> {{{
> migrations.AddConstraint(
> model_name='mymodel',
> constraint=models.CheckConstraint(
> check=models.Q(
> ('date__month_last_day__gte',
> django.db.models.expressions.F('period_start'))
> ),
> name='project_period_charge_type_task_month_budget_month_in_period'),
> ),
> }}}
>
> django **migrate command** fail indicating that `date__month_last_day`
> join is not possible
>
> {{{django.core.exceptions.FieldError: Joined field references are not
> permitted in this query}}}
>
> I'm not sure what could be done. May be the documentation should warn to
> not use custom lookup/transform in CheckContraint.
New description:
Using a custom lookup or transform in a **CheckContraint** make migrate
command fail.
{{{
class MonthLastDay(Transform):
lookup_name = "month_last_day"
output_field = DateField()
template = "(DATE_TRUNC('month', %(expressions)s) + (interval '1 month
- 1 day'))::date"
DateField.register_lookup(MonthLastDay)
class MyModel(models.Model):
class Meta:
constraints = [
CheckConstraint(check=Q(date__month_last_day__gte=F("period_start")),
name="date_month_end_after_period_start"),
]
date = models.DateField()
period_start = models.DateField()
}}}
django **makemigrations command** does not indicate any issue with this
and make use of `month_last_day` lookup as if it know about it.
Migration command for this contraint:
{{{
migrations.AddConstraint(
model_name='mymodel',
constraint=models.CheckConstraint(
check=models.Q(
('date__month_last_day__gte',
django.db.models.expressions.F('period_start'))
),
name='date_month_end_after_period_start'),
),
}}}
django **migrate command** fail indicating that `date__month_last_day`
join is not possible
{{{django.core.exceptions.FieldError: Joined field references are not
permitted in this query}}}
I'm not sure what could be done. May be the documentation should warn to
not use custom lookup/transform in CheckContraint.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33192#comment:3>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/071.311f8e120ada17d0fa7e63f5d6f89ceb%40djangoproject.com.