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

djangonaut
  • 7,233
  • 5
  • 37
  • 52

1 Answers1

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

justdavey
  • 226
  • 1
  • 4
  • How would adding a `order_by` affect an empty queryset? – djangonaut May 04 '16 at 13:44
  • Adding an empty .order_by() will remove any ordering that may affect the result. A similar problem is described here: http://stackoverflow.com/questions/13816062/annotate-in-sqlite – justdavey May 06 '16 at 14:20