0
0
mirror of https://github.com/django/django.git synced 2024-11-24 11:48:10 +01:00

Fixed #21603 -- Fixed complex RawQuerySets queries on some versions of SQLite.

This commit is contained in:
Alex Hill 2013-12-13 14:04:28 +08:00 committed by Tim Graham
parent edcc75e5ac
commit 938da36cb1
4 changed files with 30 additions and 1 deletions

View File

@ -1267,6 +1267,14 @@ class BaseDatabaseIntrospection(object):
"""
return name
def column_name_converter(self, name):
"""
Apply a conversion to the column name for the purposes of comparison.
Uses table_name_converter() by default.
"""
return self.table_name_converter(name)
def table_names(self, cursor=None):
"""
Returns a list of names of all tables that exist in the database.

View File

@ -68,6 +68,21 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
return [FieldInfo(info['name'], info['type'], None, info['size'], None, None,
info['null_ok']) for info in self._table_info(cursor, table_name)]
def column_name_converter(self, name):
"""
SQLite will in some cases, e.g. when returning columns from views and
subselects, return column names in 'alias."column"' format instead of
simply 'column'.
Affects SQLite < 3.7.15, fixed by http://www.sqlite.org/src/info/5526e0aa3c
"""
# TODO: remove when SQLite < 3.7.15 is sufficiently old.
# 3.7.13 ships in Debian stable as of 2014-03-21.
if self.connection.Database.sqlite_version_info < (3, 7, 15):
return name.split('.')[-1].strip('"')
else:
return name
def get_relations(self, cursor, table_name):
"""
Returns a dictionary of {field_index: (field_index_other_table, other_table)}

View File

@ -66,7 +66,7 @@ class RawQuery(object):
def get_columns(self):
if self.cursor is None:
self._execute_query()
converter = connections[self.using].introspection.table_name_converter
converter = connections[self.using].introspection.column_name_converter
return [converter(column_meta[0])
for column_meta in self.cursor.description]

View File

@ -239,3 +239,9 @@ class RawQueryTests(TestCase):
def test_query_count(self):
self.assertNumQueries(1, list, Author.objects.raw("SELECT * FROM raw_query_author"))
def test_subquery_in_raw_sql(self):
try:
list(Book.objects.raw('SELECT "id" FROM (SELECT * FROM raw_query_book WHERE paperback) sq'))
except InvalidQuery:
self.fail("Using a subquery in a RawQuerySet raised InvalidQuery")