Flask SqlAlchemy join two models without foreign key MYSQL

夙愿已清 提交于 2019-12-04 05:45:59

You need to tell SQLAlchemy how to join the tables. Try something like this:

result = db.session.query(Users).join(TimeOff,Users.userName==TimeOff.userName)

To improve upon @Matt Healy's answer, if you also want to be able to access attributes on the joined object you can do something like:

user, timeOff = db.session.query(Users, TimeOff).join(
    TimeOff, Users.userName == TimeOff.userName
).first()

Then timeOff.dayWork etc. will give the information you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!