Flask SqlAlchemy join two models without foreign key MYSQL

前端 未结 4 2332
余生分开走
余生分开走 2021-02-20 11:49

I am joining two models without a foreign key:

Models:

class Users(db.Model):
    __tablename__ = \"Users\"
    userName = db.Column(db.String, primary_k         


        
4条回答
  •  情深已故
    2021-02-20 11:56

    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.

提交回复
热议问题