Opened 15 years ago

Closed 9 years ago

#11329 closed Bug (wontfix)

get_count generates incorrect SQL for query sets with extra 'select' and 'where'

Reported by: bogklug Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Normal Keywords: sql, extra select, extra where, get_count
Cc: markus.magnuson@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex Gaynor)

Extending an example on extra 'select' from http://docs.djangoproject.com/en/1.0/ref/models/querysets/ with an extra 'with' and calling get_count:

Blog.objects.extra(
    select={
        'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
    },
    where=['entry_count=0']
).query.get_count()

results in OperationalError: no such column: entry_count.

The SQL for the query itself is ok. It seems that the problem is caused by get_count.

Attachments (1)

sql-get-count-with-select-extra.patch (701 bytes ) - added by joaoma@… 15 years ago.
Check for extra_select in get_count

Download all attachments as: .zip

Change History (9)

comment:1 by Alex Gaynor, 15 years ago

Description: modified (diff)

Please use the preview option. Also, are you using the internal get_count() function instead of the public count() API.

by joaoma@…, 15 years ago

Check for extra_select in get_count

comment:2 by João Marcus, 15 years ago

I am not a Django developer, but this looks like a simple patch. The get_count() function does not check for select causes included with ".extra". Attached my patch.

comment:3 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:5 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 by Markus Amalthea Magnuson, 9 years ago

Cc: markus.magnuson@… added

It is still possible to trigger this error, but it seems like aggregate() and annotate() would since long be the preferred way to carry out anything similar to this example.

Last edited 9 years ago by Markus Amalthea Magnuson (previous) (diff)

comment:8 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed

I agree that the aggregate() and annotate() methods should be used these days, so I don't see much value in fixing this issue.

Note: See TracTickets for help on using tickets.
Back to Top