﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
10942	Cannot pass a string of comma separated values or an iterable to `params` or `select_params` (e.g. for use with IN lookup).	Tai Lee	nobody	"As per the [http://groups.google.com/group/django-users/browse_thread/thread/50c1877fe8339609 google groups discussion], I'm seeing inconsistent results when I use `extra()` with `select` and `select_params` arguments compared to injecting my params directly into the `select` argument.

As per Malcolm's suggestion, I've now also tried using `django.db.connection` and `pysqlite2` to execute the raw SQL, as well as copy & pasting the generated SQL directly into the sqlite3 prompt.

Everything works except using `extra()` with `select` and `select_params` arguments.

{{{
>>> from django.contrib.auth.models import User

>>> # using `select_params` fails, with both the specified users being returned with `featured` as 0.
>>> q1 = User.objects.extra(
...     select={'featured': 'auth_user.id IN (%s)'},
...     select_params=['1,2']
... ).values('featured', 'pk', 'username').order_by('-featured', 'pk', 'username')
>>> print list(q1)
[{'username': u'admin', 'pk': 1, 'featured': 0}, {'username': u'manager', 'pk': 2, 'featured': 0}]

>>> # using only `select` works, with both the specified users being returned with `featured` as 1.
>>> q2 = User.objects.extra(
...     select={'featured': 'auth_user.id IN (%s)' % '1,2'}
... ).values('featured', 'pk', 'username').order_by('-featured', 'pk', 'username')
>>> print list(q2)
[{'username': u'admin', 'pk': 1, 'featured': 1}, {'username': u'manager', 'pk': 2, 'featured': 1}]

>>> # the sql generated for both querysets is identical.
>>> str(q1.query) == str(q2.query)
True

>>> # ths sql generated looks correct.
>>> print q1.query
SELECT (auth_user.id IN (1,2)) AS ""featured"", ""auth_user"".""id"", ""auth_user"".""username"" FROM ""auth_user"" ORDER BY ""featured"" DESC, ""auth_user"".""id"" ASC, ""auth_user"".""username"" ASC

>>> # using django to execute raw sql works as expected.
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute(str(q1.query)).fetchall()
[(1, 1, u'admin'), (1, 2, u'manager')]

>>> # using pysqlite2 to execute raw sql works as expected.
>>> from django.conf import settings
>>> from pysqlite2 import dbapi2
>>> connection = dbapi2.Connection(settings.DATABASE_NAME)
>>> cursor = dbapi2.Cursor(connection)
>>> cursor.execute(str(q1.query)).fetchall()
[(1, 1, u'admin'), (1, 2, u'manager')]

# using sqlite3 to execute raw sql works as expected.
sqlite> SELECT (auth_user.id IN (1,2)) AS ""featured"", ""auth_user"".""id"", ""auth_user"".""username"" FROM ""auth_user"" ORDER BY ""featured"" DESC, ""auth_user"".""id"" ASC, ""auth_user"".""username"" ASC
1|1|admin
1|2|manager
}}}
"		closed	Database layer (models, ORM)	dev		invalid	extra params select select_params iterable sequence		Unreviewed	0	0	0	0	0	0
