Opened 7 years ago

Closed 17 months 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@… 7 years ago.
Check for extra_select in get_count

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by Alex Gaynor

Description: modified (diff)

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

Changed 7 years ago by joaoma@…

Check for extra_select in get_count

comment:2 Changed 7 years ago by João Marcus

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 Changed 7 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:4 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:5 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 Changed 18 months ago by Markus Amalthea Magnuson

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 18 months ago by Markus Amalthea Magnuson (previous) (diff)

comment:8 Changed 17 months ago by Tim Graham

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