annotate() + order_by() == aborted transaction?

32 views
Skip to first unread message

Don Spaulding

unread,
Jun 1, 2009, 7:12:03 PM6/1/09
to Django users
Hi all,

I've got a quick use case that I think should work according to the
docs, but it's not. In this case, Domain objects have a reverse fkey
relation named "reports", and the Report model has an "updated"
field. I'd like to annotate and order Domains by the max value of
"updated" for all related reports. The following interactive session
shows the behavior pretty well.

>>> qs = Domain.objects.annotate(last_updated=Max('reports__updated')).order_by('last_updated')
>>> qs.count()
1577
>>> print qs[0].last_updated
IndexError: list index out of range
>>> qs.count()
InternalError: current transaction is aborted, commands ignored until
end of transaction block
>>> connection._rollback()

If I drop off the order_by call, things appear to work again.

>>> qs = Domain.objects.annotate(last_updated=Max('reports__updated'))
>>> qs.count()
1577
>>> print qs[0].last_updated
2009-05-28 13:25:55.027600

What am I missing here (besides a thorough understanding of
aggregation)?

Don Spaulding

unread,
Jun 3, 2009, 4:03:29 PM6/3/09
to Django users
bump.

Can anyone tell me if this looks like a bug in Django?

Alex Gaynor

unread,
Jun 3, 2009, 4:05:25 PM6/3/09
to django...@googlegroups.com
It looks like a bug in django at first glance.  Out of curiosity what version of Django is it, since it looks a tiny bit like an old bug with queryset chaining, but I think that was fixed even before the first beta.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Don Spaulding

unread,
Jun 3, 2009, 6:22:43 PM6/3/09
to Django users


On Jun 3, 3:05 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
>>> django.get_version()
u'1.1 beta 1 SVN-10916'

Alex Gaynor

unread,
Jun 3, 2009, 6:25:10 PM6/3/09
to django...@googlegroups.com
Ok, based on what you pasted it's almost certainly a bug in Django.  Please file a bug, and if you can try to write up a testcase within the Django test framework, but if not try to provide a minimal set of Models and code to reproduce the bug.

Don Spaulding

unread,
Jun 3, 2009, 6:59:35 PM6/3/09
to Django users
Hmm, after updating to u'1.1 beta 1 SVN-10921' I'm now getting a
psycopg2.ProgrammingError: ORDER BY "last_updated" is ambiguous.

I've put the SQL it generates (I've only sanitized the two table
names) up at http://dpaste.com/51213/

Don Spaulding

unread,
Jun 3, 2009, 8:11:56 PM6/3/09
to Django users
And of course, therein lies my problem, I've gone and tried to
annotate each record with a 'last_updated' calculation, and the model
already has an actual 'last_updated' field. The error only shows up
when django tries to tell the SQL engine to order_by something that's
been defined twice in the query.

I'll raise a ticket on the tracker about possibly raising an exception
instead of allowing users (stupid ones like me) to clobber their own
namespaces.

Don Spaulding

unread,
Jun 3, 2009, 8:57:34 PM6/3/09
to Django users
Reply all
Reply to author
Forward
0 new messages