Opened 16 years ago
Closed 10 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 )
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)
Change History (9)
comment:1 by , 16 years ago
| Description: | modified (diff) | 
|---|
by , 16 years ago
| Attachment: | sql-get-count-with-select-extra.patch added | 
|---|
Check for extra_select in get_count
comment:2 by , 16 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 , 16 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
comment:4 by , 15 years ago
| Severity: | → Normal | 
|---|---|
| Type: | → Bug | 
comment:7 by , 10 years ago
| Cc: | 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.
comment:8 by , 10 years ago
| Resolution: | → wontfix | 
|---|---|
| Status: | new → closed | 
I agree that the aggregate() and annotate() methods should be used these days, so I don't see much value in fixing this issue.
Please use the preview option. Also, are you using the internal get_count() function instead of the public count() API.