0

I'm new to python. I have a doubt in connecting multiple databases and in writing join query with it. My raw query is:

 SELECT FD.number, FD.options, DATE_FORMAT(TD.date,"%y-%m-%d") as date, 
T.id,S.name, S.id, CP.id, FL.id from finance.reports FD inner join

finance.details TD on FD.number=TD.number inner join app.trans T on T.id = FD.id inner join app.students S on S.id = T.student_id inner join internal.c_map CP on

 FD.options=CP.mode left join internal.trans_logger FL on FL.number = FD.number Where S.id = "181" order by FD.number desc limit 50

Here I used three databases, finance, app, internal And the tables from finance db are reports and details, tables from app db are students and trans, tables from internal are trans_logger and c_map

That raw query is working. But I need to write the same query using orm. How to bind the databases and to achieve this.

I tried like this, But it's not working,

Session = sessionmaker()
Session.configure(binds={
    T1: create_engine("finance"),
    T2: create_engine("internal"),
    T3: create_engine("app")
}
session = Session()
q = (session.query(T1)
            .join(T2, T1.reports.number == T2.details.number)
            .join(T3, T1.students.id == T2.trans.student_id)
            .limit(50)
)

I've missed something. Kindly help me to achieve that raw query in orm.

4
  • Not working okay. Any errors / output that you can share. Commented Jul 8, 2019 at 13:24
  • Pretty close to being a dupe of Join tables in two databases using SQLAlchemy Commented Jul 8, 2019 at 13:49
  • @NiranjRajasekaran Bro, Actually It's giving me an output of the wrong query. No errors. Commented Jul 9, 2019 at 3:55
  • If you can help, kindly do. I really needed it @IljaEverilä Commented Jul 9, 2019 at 4:02

0

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.