1

Following model relations:

  • classA has a non-nullable FK-Field to classC.
  • classB has a nullable FK-Field to classC.

I need a list of classA-records that have zero classB-records connected to the common classC-record.

In postgres both queries give me what I want, while in sqlite they both give me a empty result:

ClassA.objects.annotate(num_classb=Count('classc__classbs')).filter(num_classb=0)

ClassA.objects.filter(classc__classbs__isnull=True)

Where do I go wrong? I did not find something about differences in the handling of such filters/annotations between postgres and sqlite

1
  • posting the models mignt help Commented May 4, 2016 at 14:54

1 Answer 1

1

Have you tried appending .order_by()?

Source: https://docs.djangoproject.com/en/1.9/topics/db/aggregation/#interaction-with-default-ordering-or-order-by

Sign up to request clarification or add additional context in comments.

2 Comments

How would adding a order_by affect an empty queryset?
Adding an empty .order_by() will remove any ordering that may affect the result. A similar problem is described here: stackoverflow.com/questions/13816062/annotate-in-sqlite

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.