1

We are testing the possibility to implement SQLAlchemy to handle our database work. In some instances I need to join a database to a clone of itself (with potentially different data, of course).

An example of the SQL I need to replicate is as follows:

SELECT lt.name, lt.date, lt.type
FROM dbA.dbo.TableName as lt
LEFT JOIN dbB.dbo.TableName as rt
    ON lt.name = rt.name
    AND lt.date = rt.date
WHERE rt.type is NULL

So far I have tried using the join object but I can't get it to not spit the entire join out. I have also tried various .join() methods based on the tutorial here: http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html and I keep getting an AttributeError: "mapper" or not what I'm looking for.

The issues I'm running into is that I need to not only join on multiple fields, but I can't have any foreign key relationships built into the objects or tables.

1

1 Answer 1

1

Thanks to Kay's like I think I figured out the solution.

It looks like it can be solved by:

session.query(dbA_TableName).outerjoin(
   dbB_TableName,
   and_(dbA_TableName.name == dbB_TableName.name",
   dbA_TableName.date == dbB_TableName.date")
).filter("dbB_TableName.type is NULL")`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.