Commits

Anonymous committed 90acd39

make auto-correlation the same for Query & select()

Comments (0)

Files changed (3)

lib/sqlalchemy/orm/query.py

                         context.whereclause,
                         from_obj=froms,
                         use_labels=labels,
-                        correlate=False,
                         # TODO: this order_by is only needed if 
                         # LIMIT/OFFSET is present in self._select_args,
                         # else the application on the outside is enough
                             from_obj=froms,
                             use_labels=labels,
                             for_update=for_update,
-                            correlate=False,
                             order_by=context.order_by,
                             **self._select_args
                         )

lib/sqlalchemy/orm/strategies.py

     ):
         # reformat the original query
         # to look only for significant columns
-        q = orig_query._clone()
+        q = orig_query._clone().correlate(None)
 
         # TODO: why does polymporphic etc. require hardcoding 
         # into _adapt_col_list ?  Does query.add_columns(...) work

test/orm/test_froms.py

         configure_mappers()
 
 
+class QueryCorrelatesLikeSelect(QueryTest, AssertsCompiledSQL):
+
+    query_correlated = "SELECT users.name AS users_name, " \
+        "(SELECT count(addresses.id) AS count_1 FROM addresses " \
+        "WHERE addresses.user_id = users.id) AS anon_1 FROM users"
+
+    query_not_correlated = "SELECT users.name AS users_name, " \
+        "(SELECT count(addresses.id) AS count_1 FROM addresses, users " \
+        "WHERE addresses.user_id = users.id) AS anon_1 FROM users"
+
+    def test_as_scalar_select_auto_correlate(self):
+        addresses, users = self.tables.addresses, self.tables.users
+        query = select(
+            [func.count(addresses.c.id)], 
+            addresses.c.user_id==users.c.id
+        ).as_scalar()
+        query = select([users.c.name.label('users_name'), query])
+        self.assert_compile(query, self.query_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_as_scalar_select_explicit_correlate(self):
+        addresses, users = self.tables.addresses, self.tables.users
+        query = select(
+            [func.count(addresses.c.id)], 
+            addresses.c.user_id==users.c.id
+        ).correlate(users).as_scalar()
+        query = select([users.c.name.label('users_name'), query])
+        self.assert_compile(query, self.query_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_as_scalar_select_correlate_off(self):
+        addresses, users = self.tables.addresses, self.tables.users
+        query = select(
+            [func.count(addresses.c.id)], 
+            addresses.c.user_id==users.c.id
+        ).correlate(None).as_scalar()
+        query = select([ users.c.name.label('users_name'), query])
+        self.assert_compile(query, self.query_not_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_as_scalar_query_auto_correlate(self):
+        sess = create_session()
+        Address, User = self.classes.Address, self.classes.User
+        query = sess.query(func.count(Address.id))\
+            .filter(Address.user_id==User.id)\
+            .as_scalar()
+        query = sess.query(User.name, query)
+        self.assert_compile(query, self.query_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_as_scalar_query_explicit_correlate(self):
+        sess = create_session()
+        Address, User = self.classes.Address, self.classes.User
+        query = sess.query(func.count(Address.id))\
+            .filter(Address.user_id==User.id)\
+            .correlate(self.tables.users)\
+            .as_scalar()
+        query = sess.query(User.name, query)
+        self.assert_compile(query, self.query_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_as_scalar_query_correlate_off(self):
+        sess = create_session()
+        Address, User = self.classes.Address, self.classes.User
+        query = sess.query(func.count(Address.id))\
+            .filter(Address.user_id==User.id)\
+            .correlate(None)\
+            .as_scalar()
+        query = sess.query(User.name, query)
+        self.assert_compile(query, self.query_not_correlated,
+            dialect=default.DefaultDialect()
+        )
+
+
 class RawSelectTest(QueryTest, AssertsCompiledSQL):
     """compare a bunch of select() tests with the equivalent Query using straight table/columns.
 
             )
 
         # a little tedious here, adding labels to work around Query's auto-labelling.
-        # also correlate needed explicitly.  hmmm.....
         # TODO: can we detect only one table in the "froms" and then turn off use_labels ?
         s = sess.query(addresses.c.id.label('id'), addresses.c.email_address.label('email')).\
-            filter(addresses.c.user_id==users.c.id).correlate(users).statement.alias()
+            filter(addresses.c.user_id==users.c.id).statement.alias()
 
         self.assert_compile(sess.query(users, s.c.email).select_from(users.join(s, s.c.id==users.c.id)).with_labels().statement, 
                 "SELECT users.id AS users_id, users.name AS users_name, anon_1.email AS anon_1_email "