nshafer / django-hashid-field

Django Model Field that uses Hashids to obscure the value
MIT License
370 stars 40 forks source link

subquery has too many columns #39

Closed wowkin2 closed 4 years ago

wowkin2 commented 4 years ago

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-column

Can be closed.