Opened 6 hours ago

Last modified 4 hours ago

#36954 assigned Bug

Filtering an ManyToManyField causes the `.values` call to be filtered

Reported by: Ryan "RB" Barnes Owned by: Mahi Singhal
Component: Database layer (models, ORM) Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (1)

comment:1 by Mahi Singhal, 4 hours ago

Owner: set to Mahi Singhal
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top