rohitjain-rj / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
0 stars 0 forks source link

Tag.object.usage_for_queryset() fails when queryset is filtered by with_all() with one tag #230

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Failed on SQLite3 backend

Example:

Valid query:
Tag.objects.usage_for_queryset(Trip.tagged.with_all(['mountain','snow']).fi
lter(is_public=True).order_by('-created_at'))

Failing query:
Tag.objects.usage_for_queryset(Trip.tagged.with_all(['mountain']).filter(is
_public=True).order_by('-created_at'))

OperationalError                          Traceback (most recent call last)
/home/ubu/devel/projekty/ntk/apps/django-tagging/tagging/models.pyc in 
usage_for_queryset(self, queryset, counts, min_count)
    169         else:
    170             extra_criteria = ''
--> 171         return self._get_usage(queryset.model, counts, min_count, 
extra_joins, extra_criteria, params)
    172
    173     def related_for_model(self, tags, model, counts=False, 
min_count=None):

/home/ubu/devel/projekty/ntk/apps/django-tagging/tagging/models.pyc in 
_get_usage(self, model, counts, min_count, extra_joins, extra_criteria, 
params)
    113
    114         cursor = connection.cursor()
--> 115         cursor.execute(query % (extra_joins, extra_criteria, 
min_count_sql), params)
    116         tags = []
    117         for row in cursor.fetchall():

/usr/lib/python2.6/site-packages/Django-1.1-
py2.6.egg/django/db/backends/sqlite3/base.pyc in execute(self, query, 
params)
    191     def execute(self, query, params=()):
    192         query = self.convert_query(query, len(params))
--> 193         return Database.Cursor.execute(self, query, params)
    194
    195     def executemany(self, query, param_list):

OperationalError: ambiguous column name: tagging_taggeditem.content_type_id

This error is caused by "Optimisation for single tag" in 
TaggedItemManager.get_by_model() method.

Problem can be solved by simply removing optimisation for single tag. Patch 
attached.

Original issue reported on code.google.com by marcin.j...@gmail.com on 12 Dec 2009 at 6:47

Attachments:

GoogleCodeExporter commented 9 years ago
Confirmed. This fails on MySQL as well by producing malformed SQL query.

{{{
SELECT DISTINCT `tagging_tag`.id, `tagging_tag`.name
        FROM
            `tagging_tag`
            INNER JOIN `tagging_taggeditem`
                ON `tagging_tag`.id = `tagging_taggeditem`.tag_id
            INNER JOIN `posts_post`
                ON `tagging_taggeditem`.object_id = `posts_post`.`id`
            , `tagging_taggeditem`
            ^^^^^^^^^^^^^^^^^^^^^^
        WHERE `tagging_taggeditem`.content_type_id = 11
            AND `posts_post`.`is_confirmed` = %s 
        GROUP BY `tagging_tag`.id, `tagging_tag`.name

        ORDER BY `tagging_tag`.name ASC
}}}

Original comment by rob...@webcookies.org on 24 Feb 2010 at 2:08