From: Vladimir Magamedov Date: Wed, 4 Sep 2013 08:43:40 +0000 (+0300) Subject: Fixed Query.exists() method for the case, when query doesn't have any filters applied. X-Git-Tag: rel_0_8_3~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d1bd984e603f3c1910967c2b528bf1ce3cf999c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixed Query.exists() method for the case, when query doesn't have any filters applied. Conflicts: doc/build/changelog/changelog_09.rst --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 34fa58e0cd..cfdd26ad59 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -6,6 +6,13 @@ .. changelog:: :version: 0.8.3 + .. change:: + :tags: bug, orm + :tickets: 2818 + + Fixed bug where :meth:`.Query.exists` failed to work correctly + without any WHERE criterion. Courtesy Vladimir Magamedov. + .. change:: :tags: bug, sql :tickets: 2811 diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 8f835cd30e..499bdeb4d7 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -2492,7 +2492,7 @@ class Query(object): .. versionadded:: 0.8.1 """ - return sql.exists(self.with_entities('1').statement) + return sql.exists(self.statement.with_only_columns(['1'])) def count(self): """Return a count of rows this Query would return. diff --git a/test/orm/test_query.py b/test/orm/test_query.py index 5b3d9f28ab..30d32db962 100644 --- a/test/orm/test_query.py +++ b/test/orm/test_query.py @@ -1737,8 +1737,17 @@ class ExistsTest(QueryTest, AssertsCompiledSQL): def test_exists(self): User = self.classes.User sess = create_session() - q1 = sess.query(User).filter(User.name == 'fred') + + q1 = sess.query(User) self.assert_compile(sess.query(q1.exists()), + 'SELECT EXISTS (' + 'SELECT 1 FROM users' + ') AS anon_1', + dialect=default.DefaultDialect() + ) + + q2 = sess.query(User).filter(User.name == 'fred') + self.assert_compile(sess.query(q2.exists()), 'SELECT EXISTS (' 'SELECT 1 FROM users WHERE users.name = :name_1' ') AS anon_1',