Home > Software design >  Left Join in Flask SQLAlchemy
Left Join in Flask SQLAlchemy

Time:08-09

I want to do a left-join in flask-sql alchemy.

SELECT * FROM CARLOGS
LEFT JOIN vehicles ON vehicles.id = CARLOGS.vehicle_id;

Now In Flask-sqlalchemy, I have tried the simple join, which works

CarLogs.query.join(Vehicle, CarLogs.vehicle_id == Vehicle.id)

But, if I want to do a left join, I can not find any option on the documentation of flask-sqlalchemy.

db.session.query(CarLogs, Vehicle).leftjoin(CarLogs.vehicle_id == Vehicle.id)) # inspired by a so answer, gives error

or

CarLogs.query.leftjoin(Vehicle, CarLogs.vehicle_id == Vehicle.id) # Gives error

Error message:

AttributeError: 'BaseQuery' object has no attribute 'leftjoin'

CodePudding user response:

You can try join with isouter parameter

CarLogs.query.join(Vehicle, CarLogs.vehicle_id == Vehicle.id, isouter=True)
  • Related