0
votes

According to the SQLAlchemy documentation one can do this:

address_subq = session.query(Address).\
            filter(Address.email_address == '[email protected]').\
            subquery()

q = session.query(User).join(address_subq, User.addresses)

SQLA docs, section "Advanced Targeting"

What I would want from the query is not the User but a tuple of (User, Address) constructed via a "left outer join" so I get tuples of (User, Address) or (User, None) if the user has no address or the (subquery?) filter removed the address from the users list of addresses if they don't match the filter's criteria.

address_subq = session.query(Address).\
            filter(Address.email_address == '[email protected]').\
            subquery()

q = session.query(User, Address).outerjoin(address_subq, User.addresses)

I adapted to example to my actual classes, but it does not work. I have tried with "correlated" and "aliased", but I do find the solution. What do I need to change to make it work?

This would be the SQL I am after:

SELECT
   user.name, addr.email
FROM
   user
      LEFT OUTER JOIN
   (SELECT
      address.name, address.user_id
   FROM
      address
   WHERE
      address.email LIKE '%foo.com%') AS addr ON user.id = addr.user_id
1

1 Answers

1
votes

It would be more clear if you had included the target SQL statement that you wish to execute, but I am assuming you want to query for all the fields in the user along with its address where the the email_address matches some specific value. If this is what you want, a simple join will suffice and a subquery can be completely avoided. For sqlalchemy, it is possible to select explicitly from a table using select_from, and that has an example which can probably be adapted to something like so for your specific use case:

>>> q = session.query(User, Address).select_from(User).join(Address).filter(
...     Address.email_address == '[email protected]')
>>> print(q)
SELECT users.id AS users_id, users.name AS users_name, ... 
    addresses.email_address ..., addresses.user_id ...,
FROM users JOIN addresses ON users.id = addresses.user_id 
WHERE addresses.email_address = ?

Which should get back a result with the user along with its address for the given email_address.

Of course (as per comments here), since User in this case is in the primary query, the select_from can be omitted for this particular instance, but this is still useful demonstration on how the queries are constructed by sqlalchemy.


If you want specific columns (originally, you were not clear by what you meant by a tuple of (User, Address), and that implies the entire class which is what is being returned already), simply pass the attribute that you want as documented in the querying section.

For your intended part, you need to make use of the subquery as the query parameter instead of the Address, and with manual joins as specified again in the tutorial under the Using Subqueries section.

The address_subq in your question can be used as is, but if you want to replace your inner query to be more like what you later specified, try:

address_subq = session.query(Address).filter(
    Address.email_address.like('%@foo.com')).subquery()

The final query that satisfies your requirement will be:

q = session.query(
     User.name, address_subq.c.email_address
).outerjoin(
     address_subq, User.id==address_subq.c.user_id)

Printing and executing the query (the print output has been modified for readability)

>>> print(q)
SELECT users.name AS users_name, anon_1.email_address AS anon_1_email_address 
FROM users LEFT OUTER JOIN (
    SELECT addresses.id AS id,
           addresses.email_address AS email_address,
           addresses.user_id AS user_id 
    FROM addresses 
    WHERE addresses.email_address LIKE ?
) AS anon_1 ON users.id = anon_1.user_id
>>> q.all()
[('ed', '[email protected]'), ('wendy', None), ('mary', None), ('fred', None), ('jack', None)]