Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#11962 closed (duplicate)

Annotate with "extra" triggers Traceback (bad SQL)

Reported by: erikcw Owned by:
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords: annotate, extra
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have a query which performs a fairly complex "extra query" (about 10 sub-select queries). When I added the annotate() call to the chain, I started getting the following traceback.

#select is a dict of SELECTS.
Profile.profilekeyword_set.select_related().filter(approved=True).extra(select=select).annotate(anchors=Count('keyword__anchor')).select_related()
....
/usr/lib/python2.5/pprint.pyc in _safe_repr(object, context, maxlevels, level)
    290         return format % _commajoin(components), readable, recursive
    291 
--> 292     rep = repr(object)
    293     return rep, (rep and not rep.startswith('<')), False
    294 

/usr/lib/python2.5/site-packages/django/db/models/query.pyc in __repr__(self)
     66 
     67     def __repr__(self):
---> 68         data = list(self[:REPR_OUTPUT_SIZE + 1])
     69         if len(data) > REPR_OUTPUT_SIZE:
     70             data[-1] = "...(remaining elements truncated)..."

/usr/lib/python2.5/site-packages/django/db/models/query.pyc in __len__(self)
     81                 self._result_cache = list(self.iterator())
     82         elif self._iter:
---> 83             self._result_cache.extend(list(self._iter))
     84         return len(self._result_cache)
     85 

/usr/lib/python2.5/site-packages/django/db/models/query.pyc in iterator(self)
    236             model_cls = deferred_class_factory(self.model, skip)
    237 
--> 238         for row in self.query.results_iter():
    239             if fill_cache:
    240                 obj, _ = get_cached_row(self.model, row,

/usr/lib/python2.5/site-packages/django/db/models/sql/query.pyc in results_iter(self)
    285         resolve_columns = hasattr(self, 'resolve_columns')
    286         fields = None
--> 287         for rows in self.execute_sql(MULTI):
    288             for row in rows:
    289                 if resolve_columns:

/usr/lib/python2.5/site-packages/django/db/models/sql/query.pyc in execute_sql(self, result_type)
   2367                 return
   2368         cursor = self.connection.cursor()
-> 2369         cursor.execute(sql, params)
   2370 
   2371         if not result_type:

ProgrammingError: syntax error at or near "SELECT"
LINE 1: ...keyword"."keyword", "keywords_keyword"."created", SELECT sea...
                                                             ^

It looks like a comma is being added after "keywords_keyword"."created" instead of a ")" (just a guess).

Change History (3)

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Duplicate of #11104. Please search trac before filing a new ticket.

comment:2 Changed 6 years ago by Alex

  • Resolution set to duplicate
  • Status changed from new to closed

comment:3 Changed 3 years ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top