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.

davidism
  • 121,510
  • 29
  • 395
  • 339
Gapp
  • 9
  • 2

0 Answers0