A Subquery filter on models.Obj, e.g., filter(obj__in=Subquery(...)) ,requires that the subquery return exactly one column populated with Obj.pk.
Documentation suggests using objects.values('pk')
However, if the models.Obj.Meta.ordering is set, then those ordering fields are included with the subquery results, raising the exception:
django.db.utils.ProgrammingError: subquery has too many columns
This is expected behavior, but confusing and not directly obvious in the code. Could be more clearly documented at:
https://docs.djangoproject.com/en/dev/ref/models/expressions/#limiting-a-subquery-to-a-single-column
Had issue described on DjangoProject issue tracker, but just after switching to use this battery (django-hashid-field). So decided to share and save time for others. https://code.djangoproject.com/ticket/30047?cversion=0&cnum_hist=9
A Subquery filter on models.Obj, e.g.,
filter(obj__in=Subquery(...))
,requires that the subquery return exactly one column populated with Obj.pk.Documentation suggests using
objects.values('pk')
However, if the models.Obj.Meta.ordering is set, then those ordering fields are included with the subquery results, raising the exception:
django.db.utils.ProgrammingError: subquery has too many columns
This is expected behavior, but confusing and not directly obvious in the code. Could be more clearly documented at: https://docs.djangoproject.com/en/dev/ref/models/expressions/#limiting-a-subquery-to-a-single-columnCan be closed.