python - Programming error with order by and distinct on Django with PostgreSQL -
there lot of similar posts none hit @ trying to. distinct has use same fields order_by, fine.
so have following query:
q = mymodel.objects.order_by('field1', 'field2', '-tstamp').distinct('field1', 'field2') ultimately trying find latest entry in table combinations of field1 , field2. order_by think should, , that's great. when distinct following error.
programmingerror: select distinct on expressions must match initial order expressions ultimately seems sql problem (not django one). looking @ django docs distinct, shows can , can't work. that
q = mymodel.objects.order_by('field1', 'field2', '-tstamp').distinct('field1') will work (...and does). don't understand when add on field2 in same order done in order_by still same result. appreciated
edit: notice if do
q = mymodel.objects.order_by('field1', 'field2', '-tstamp').distinct('field1', 'field2', 'tstamp') # or without - in order_by it still raises same error, though docs suggest should work fine
was able query run using pk's in order_by()
q = mymodel.objects.order_by('field1__pk', 'field2__pk', '-tstamp').distinct('field1__pk', 'field2__pk') apparently when not ordinary types orderby doesn't play super nice. using pk's of objects seems work
Comments
Post a Comment