﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33192	It is not possible to use a custom lookup/transorm in a CheckConstraint	Fabien MICHEL	nobody	"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."	Bug	new	Database layer (models, ORM)	3.1	Normal		lookup, transform, CheckContraint, migrate		Unreviewed	0	0	0	0	0	0
