#20250: AttributeError when filtering annotated queryset with negated Q (via fk)
----------------------------------------------+----------------------------
     Reporter:  jan.koutny@…                  |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.4
     Severity:  Normal                        |   Keywords:  annotate
 Triage Stage:  Unreviewed                    |  AttributeError
Easy pickings:  0                             |  Has patch:  0
                                              |      UI/UX:  0
----------------------------------------------+----------------------------
 If these conditions are met:
 * annotated queryset
 * Q is filtering attribute related via foreign key
 * Q is negation

 Then {{{AttributeError: 'NoneType' object has no attribute 'startswith'}}}
 exception is throwed. Stack looks the same as in 12687 (fixed), see below.

 Note that *all* conditions above are necessary, code works if one omitted.
 Reproducible with Django 1.4.5, python 2.6.8.

 {{{
 Traceback (most recent call last):
   File "bug.py", line 16, in <module>
     print qst.query
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py",
 line 167, in __str__
     sql, params = self.sql_with_params()
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py",
 line 175, in sql_with_params
     return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
   File "/usr/lib/python2.6/site-
 packages/django/db/models/sql/compiler.py", line 82, in as_sql
     where, w_params = self.query.where.as_sql(qn=qn,
 connection=self.connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 91, in as_sql
     sql, params = child.as_sql(qn=qn, connection=connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 91, in as_sql
     sql, params = child.as_sql(qn=qn, connection=connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 91, in as_sql
     sql, params = child.as_sql(qn=qn, connection=connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 91, in as_sql
     sql, params = child.as_sql(qn=qn, connection=connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 91, in as_sql
     sql, params = child.as_sql(qn=qn, connection=connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 94, in as_sql
     sql, params = self.make_atom(child, qn, connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 154, in make_atom
     field_sql = self.sql_for_columns(lvalue, qn, connection)
   File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py",
 line 227, in sql_for_columns
     lhs = '%s.%s' % (qn(table_alias), qn(name))
   File "/usr/lib/python2.6/site-
 packages/django/db/models/sql/compiler.py", line 49, in
 quote_name_unless_alias
     r = self.connection.ops.quote_name(name)
   File "/usr/lib/python2.6/site-
 packages/django/db/backends/sqlite3/base.py", line 145, in quote_name
     if name.startswith('"') and name.endswith('"'):
 AttributeError: 'NoneType' object has no attribute 'startswith'
 }}}


 Code snippet to reproduce the issue, bug.py:
 {{{
 from bug.models import *
 from django.db.models import Q,Count

 qobj=~Q(rtom3__x=0) #TOXIC
 #qobj=~Q(x=0) #OK
 #qobj=Q(rtom3__x=0) #OK

 qst =  M1.objects.annotate(Count('m2')) #TOXIC
 #qst =  M1.objects.all() #OK

 qst = qst.filter(qobj)

 #Gracefully returns [], no attempt to hit the database!!!!
 #print qst

 #Throws an exception!
 print qst.query
 }}}

 and models.py:
 {{{
 from django.db import models

 class M1(models.Model):
         rtom3 = models.ForeignKey("M3")
         x = models.IntegerField()

 class M2(models.Model):
         rtom1 = models.ForeignKey("M1")

 class M3(models.Model):
         x = models.IntegerField()
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20250>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to