From 611f7f4e94026926c8c85717e1a2d481bd2c5657 Mon Sep 17 00:00:00 2001 From: Eric Atkin Date: Wed, 28 Feb 2018 16:00:38 -0500 Subject: Add Query.enable_single_entity() Added new feature :meth:`.Query.only_return_tuples`. Causes the :class:`.Query` object to return keyed tuple objects unconditionally even if the query is against a single entity. Pull request courtesy Eric Atkin. Change-Id: Ib0b7f5f78431aa68082e5b200ed577daa4222336 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/425 --- lib/sqlalchemy/orm/loading.py | 3 ++- lib/sqlalchemy/orm/query.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/sqlalchemy/orm/loading.py b/lib/sqlalchemy/orm/loading.py index 2628093e0..3599aa3e7 100644 --- a/lib/sqlalchemy/orm/loading.py +++ b/lib/sqlalchemy/orm/loading.py @@ -37,7 +37,8 @@ def instances(query, cursor, context): filtered = query._has_mapper_entities - single_entity = len(query._entities) == 1 and \ + single_entity = not query._only_return_tuples and \ + len(query._entities) == 1 and \ query._entities[0].supports_single_entity if filtered: diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 1e7f8a266..043be5539 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -70,6 +70,7 @@ class Query(object): """ + _only_return_tuples = False _enable_eagerloads = True _enable_assertions = True _with_labels = False @@ -603,6 +604,16 @@ class Query(object): def __clause_element__(self): return self.enable_eagerloads(False).with_labels().statement + @_generative() + def only_return_tuples(self, value): + """When set to True, the query results will always be a tuple, + specifically for single element queries. The default is False. + + . .. versionadded:: 1.2.5 + + """ + self._only_return_tuples = value + @_generative() def enable_eagerloads(self, value): """Control whether or not eager joins and subqueries are -- cgit v1.2.1