Opened 5 years ago

Closed 5 years ago

#15239 closed (duplicate)

when I combine two Q objects with | operator it ends up with AND being used in sql query.

Reported by: muaddib@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by russellm)

Hello. I created two querysets which works fine.

Invoice.objects.annotate(mysum=Sum('payments__payment')).filter(Q(mysum__lte=F('invoice_total')) )

queryset1 returns objects with mysum less then invoice_total. mysum sums payments for the particular invoice.
queryset2 catches non-numeric objects with 'None' as mysum value. This happens when there no payments for the particular invoice.

Fields used:
invoice_total is a decimal field.
payments__payment is also decimal (in related_set).

Now to combine those two querysets into one i use Q logic.

Invoice.objects.annotate(mysum=Sum('payments__payment')).filter(Q(mysum=None) | Q(mysum__lte=F('invoice_total')) )

Unfortunatly it doesnt work (returned queryset is empty). In sql log i can see that those two conditions above are always being glued with default AND (should be ORed because I use Q | Q, not Q & Q).

Database is a postresql 8.3.

Change History (1)

comment:1 Changed 5 years ago by russellm

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #15240

Note: See TracTickets for help on using tickets.
Back to Top