0

I'm migrating my project to Django 1.8 and I am receiving an error related to 'johnny cache. Specifically in 'johnny/cache.py/'.

Error: lib/python2.7/site-packages/johnny/cache.py", line 87, in get_tables_for_query tables = set([v[0] for v in getattr(query, 'alias_map', {}).values()])

TypeError: 'BaseTable' object does not support indexing

I have included my code below for the function where the error is originating from. Advice no whether I should use something other than 'johnny -cache' for caching would be helpful and/or info as to what is the meaning of this error and how to fix it. Thank you!

def get_tables_for_query(query):
"""
Takes a Django 'query' object and returns all tables that will be used in
that query as a list.  Note that where clauses can have their own
querysets with their own dependent queries, etc.
"""
from django.db.models.sql.where import WhereNode, SubqueryConstraint
from django.db.models.query import QuerySet
tables = set([v[0] for v in getattr(query, 'alias_map', {}).values()])


def get_sub_query_tables(node):
    query = node.query_object
    if not hasattr(query, 'field_names'):
        query = query.values(*node.targets)
    else:
        query = query._clone()
    query = query.query
    return set([v[0] for v in getattr(query, 'alias_map',{}).values()])

def get_tables(node, tables):
    if isinstance(node, SubqueryConstraint):
        return get_sub_query_tables(node)
    for child in node.children:
        if isinstance(child, WhereNode):  # and child.children:
            tables = get_tables(child, tables)
        elif not hasattr(child, '__iter__'):
            continue
        else:
            for item in (c for c in child if isinstance(c, QuerySet)):
                tables += get_tables_for_query(item.query)
    return tables



if query.where and query.where.children:
    where_nodes = [c for c in query.where.children if isinstance(c, (WhereNode, SubqueryConstraint))]
    for node in where_nodes:
        tables += get_tables(node, tables)

return list(set(tables))
Roshan
  • 60
  • 10
  • What is that line supposed to do? The offender here is `v[0]`, which is to say indexing. Apparently at one point during your comprehension `v` is a `BaseTable` object which has no support for this operator. – Two-Bit Alchemist Aug 03 '16 at 14:31
  • I have set up my function exactly as other johnny cache structures so I am unsure why I am getting this error. Check out how other have set up here https://github.com/jmoiron/johnny-cache/commit/7e457518ee84b29917562bda3a739acc3d407a7e – Roshan Aug 03 '16 at 14:57

1 Answers1

1

Found your problem when I looked a bit further into your library.

From the Johnny Cache documentation:

It works with Django 1.1 thru 1.4 and python 2.4 thru 2.7.

From your question:

I'm migrating my project to Django 1.8.

In fact, it looks like the library you're using is woefully out of date and no longer maintained:

Latest commit d96ea94 on Nov 10, 2014

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82