Opened 6 years ago

Last modified 3 weeks ago

#11329 new Bug

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)

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

Download all attachments as: .zip

Change History (8)

comment:1 Changed 6 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

Changed 6 years ago by joaoma@…

Check for extra_select in get_count

comment:2 Changed 6 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 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 3 weeks ago by giuliettamasina

  • 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 3 weeks ago by giuliettamasina (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top