Left Join in Flask SQLAlchemy

Question:

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'
Asked By: Ahmad Anis

||

Answers:

You can try join with isouter parameter

CarLogs.query.join(Vehicle, CarLogs.vehicle_id == Vehicle.id, isouter=True)
Answered By: Rasim Mammadov
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.