[Answer]-Postgres sql intersect with except relational division

1👍

An asnwer based on @arocks’s approach, since @rocks’s annotation is wrong.

Conversation.objects.annotate(user_count=Count('attendee')).filter(user_count=2, attendee__username="john").filter(attendee__username="mary")

That will return you a QuerySet of Conversation objects which have 2 members and members are mary and john

The reason you must you 2 separate filter is, your database management system need to create temporary subtables since you need to use same database column username twice for filtering. Your data

👤Mp0int

Leave a comment