summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-07-14 12:26:29 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2018-07-14 13:11:53 -0400
commitfb377229cd4c4e503bde9c44b78d30ad48f3cf7e (patch)
tree028747fa14cfc3eefc4003744e371c5e809626f8 /test
parenta5d4653b6e26420a61be6e80fef8a0645cb64ceb (diff)
downloadsqlalchemy-fb377229cd4c4e503bde9c44b78d30ad48f3cf7e.tar.gz
Don't apply no-traverse to query.statement
Fixed long-standing issue in :class:`.Query` where a scalar subquery such as produced by :meth:`.Query.exists`, :meth:`.Query.as_scalar` and other derivations from :attr:`.Query.statement` would not correctly be adapted when used in a new :class:`.Query` that required entity adaptation, such as when the query were turned into a union, or a from_self(), etc. The change removes the "no adaptation" annotation from the :func:`.select` object produced by the :attr:`.Query.statement` accessor. Change-Id: I554e0e909ac6ee785ec3b3b14aaec9d235aa28cf Fixes: #4304
Diffstat (limited to 'test')
-rw-r--r--test/orm/inheritance/test_polymorphic_rel.py17
-rw-r--r--test/orm/test_froms.py36
2 files changed, 42 insertions, 11 deletions
diff --git a/test/orm/inheritance/test_polymorphic_rel.py b/test/orm/inheritance/test_polymorphic_rel.py
index e5234d254..d46448355 100644
--- a/test/orm/inheritance/test_polymorphic_rel.py
+++ b/test/orm/inheritance/test_polymorphic_rel.py
@@ -1286,10 +1286,10 @@ class _PolymorphicTestBase(object):
def test_correlation_one(self):
sess = create_session()
- # unfortunately this pattern can't yet work for PolymorphicAliased
- # and PolymorphicUnions, because the subquery does not compile
- # out including the polymorphic selectable; only if Person is in
- # the query() list does that happen.
+ # this for a long time did not work with PolymorphicAliased and
+ # PolymorphicUnions, which was due to the no_replacement_traverse
+ # annotation added to query.statement which then went into as_scalar().
+ # this is removed as of :ticket:`4304` so now works.
eq_(sess.query(Person.name)
.filter(
sess.query(Company.name).
@@ -1472,17 +1472,12 @@ class PolymorphicPolymorphicTest(
class PolymorphicUnionsTest(_PolymorphicTestBase, _PolymorphicUnions):
-
- @testing.fails()
- def test_correlation_one(self):
- super(PolymorphicUnionsTest, self).test_correlation_one()
+ pass
class PolymorphicAliasedJoinsTest(
_PolymorphicTestBase, _PolymorphicAliasedJoins):
- @testing.fails()
- def test_correlation_one(self):
- super(PolymorphicAliasedJoinsTest, self).test_correlation_one()
+ pass
class PolymorphicJoinsTest(_PolymorphicTestBase, _PolymorphicJoins):
diff --git a/test/orm/test_froms.py b/test/orm/test_froms.py
index 45656f3fc..27924c413 100644
--- a/test/orm/test_froms.py
+++ b/test/orm/test_froms.py
@@ -76,6 +76,7 @@ class QueryTest(_fixtures.FixtureTest):
class QueryCorrelatesLikeSelect(QueryTest, AssertsCompiledSQL):
+ __dialect__ = "default"
query_correlated = "SELECT users.name AS users_name, " \
"(SELECT count(addresses.id) AS count_1 FROM addresses " \
@@ -141,6 +142,41 @@ class QueryCorrelatesLikeSelect(QueryTest, AssertsCompiledSQL):
self.assert_compile(
query, self.query_not_correlated, dialect=default.DefaultDialect())
+ def test_correlate_to_union(self):
+ User = self.classes.User
+ sess = create_session()
+
+ q = sess.query(User)
+ q = sess.query(User).union(q)
+ u_alias = aliased(User)
+ raw_subq = exists().where(u_alias.id > User.id)
+ orm_subq = sess.query(u_alias).filter(u_alias.id > User.id).exists()
+
+ self.assert_compile(
+ q.add_column(raw_subq),
+ "SELECT anon_1.users_id AS anon_1_users_id, "
+ "anon_1.users_name AS anon_1_users_name, "
+ "EXISTS (SELECT * FROM users AS users_1 "
+ "WHERE users_1.id > anon_1.users_id) AS anon_2 "
+ "FROM ("
+ "SELECT users.id AS users_id, users.name AS users_name FROM users "
+ "UNION SELECT users.id AS users_id, users.name AS users_name "
+ "FROM users) AS anon_1"
+ )
+
+ # only difference is "1" vs. "*" (not sure why that is)
+ self.assert_compile(
+ q.add_column(orm_subq),
+ "SELECT anon_1.users_id AS anon_1_users_id, "
+ "anon_1.users_name AS anon_1_users_name, "
+ "EXISTS (SELECT 1 FROM users AS users_1 "
+ "WHERE users_1.id > anon_1.users_id) AS anon_2 "
+ "FROM ("
+ "SELECT users.id AS users_id, users.name AS users_name FROM users "
+ "UNION SELECT users.id AS users_id, users.name AS users_name "
+ "FROM users) AS anon_1"
+ )
+
class RawSelectTest(QueryTest, AssertsCompiledSQL):
"""compare a bunch of select() tests with the equivalent Query using