#36954: Filtering an ManyToManyField causes the `.values` call to be filtered
-------------------------------------+-------------------------------------
Reporter: Ryan "RB" Barnes | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Given two following tables:
{{{
class Technician(models.Model):
...
class Appointment(models.Model):
technicians = models.ManyToManyField(Technician)
}}}
If a have an appointment with two technicians assigned to it and create
the follow queries I get results I would not expect
{{{
tech_1 = Technician()
tech_1.save()
tech_2 = Technician()
tech_2.save()
appointment = Appointment(technicians=[tech_1, tech_2])
appointment.save()
# No issues here
r1 =
Appointment.objects.filter(id=
appointment.id).values_list('technicians',
flat=True)
assert
tech_1.id in r1
assert
tech_2.id in r1
# Now filter for a single technician
r2 = Appointment.objects.filter(id=
appointment.id,
technicians__in=[tech_1]).values_list('technicians', flat=True)
assert
tech_1.id in r2
# Fails here as tech_2 has been filtered out
assert
tech_2.id in r2
}}}
This is certainly because the SQL produced joins against the through
model, but then filters it directly while also selecting from it.
I know I can get around this but using subqueries and ArrayAggs in various
ways but it feels like a footgun that most users would miss.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36954>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.