Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes the django "in" clause require the list to have two values?
    text
    copied!<p>Question: Does the django "in" clause require the list to have two values?</p> <p>I was writing some raw queries in django and I was noticing that the same query would crash depending on the values given to it. In both cases the rawqueryset is created, the crash occurs when I try to evaluate it.</p> <p>Specifically: This queryset would crash</p> <pre><code>&lt;RawQuerySet: 'select * from (select * from flingfix_user_profile where domain=illinois and sex=F) as a left outer join (select * from flingfix_rating where rater_id=24) as b ON a.id = b.ratee_id where rater_id IS NULL and a.id not in [0] LIMIT 10'&gt; </code></pre> <p>but this queryset would not</p> <pre><code>&lt;RawQuerySet: 'select * from (select * from flingfix_user_profile where domain=illinois and sex=F) as a left outer join (select * from flingfix_rating where rater_id=24) as b ON a.id = b.ratee_id where rater_id IS NULL and a.id not in [0,1] LIMIT 10'&gt; </code></pre> <p>(The difference is [0] vs [0,1] near the end of the query)</p> <p>I am building the query using the following code:</p> <pre><code>query = 'select * from (select * from flingfix_user_profile where domain=%s and sex=%s) as a left outer join (select * from flingfix_rating where rater_id=%s) as b ON a.id = b.ratee_id where rater_id IS NULL and a.id not in %s LIMIT %s' params = ['illinois', 'F', '24', [0], 10] qs = MyModel.objects.raw(query,params) </code></pre> <p>The error does not occur until I try to evaluate the queryset. E.g. <code>qs = list(qs)</code></p> <p>Here is the error:</p> <pre><code> Traceback (most recent call last): File "&lt;console&gt;", line 1, in &lt;module&gt; File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 1548, in __iter__ query = iter(self.query) File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 72, in __iter__ self._execute_query() File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 86, in _execute_query self.cursor.execute(self.sql, self.params) File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 41, in execute return self.cursor.execute(sql, params) File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 130, in execute six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2]) File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 120, in execute return self.cursor.execute(query, args) File "/usr/lib/python2.6/site-packages/MySQLdb/cursors.py", line 173, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue DatabaseError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to yo ur MySQL server version for the right syntax to use near ') LIMIT 10' at line 1") </code></pre> <p>I mean, I could easily just ensure that the list is at least 2 elements long, but I would prefer not to. Also, I was curious as to why this would happen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload