#36025: Django ORM `__range` Filter Fails to Use Annotated Related Fields in SQL
Generation
-------------------------------------+-------------------------------------
Reporter: Aashay.Amballi | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ORM | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):
* stage: Unreviewed => Accepted
Comment:
I think I can replicate, on SQLite I get an error:
`django.db.utils.OperationalError: no such column:
appname_project.start_date`
I think I can simplify the example a bit
{{{
class Project(models.Model):
start_date = models.DateField()
end_date = models.DateField()
class WorkOrder(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE,
related_name="project_work_orders", null=True)
due_date = models.DateTimeField()
}}}
Then I get the same error with:
{{{
WorkOrder.objects.annotate(
project_id_subquery=Subquery(Project.objects.filter(id=OuterRef("project_id")).values("id")),
).aggregate(
out_of_bound_count=Count("id",
filter=Q(due_date__date__range=(F('project__start_date'),
F('project__end_date')))),
project_id_subquery_sum=Sum("project_id_subquery"),
)
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36025#comment:2>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.