Search code snippets, questions, articles...

Replace NULL with false while Selecting in SQLAlchemy

Here we are replacing active column value of user table with false if it is null
from sqlalchemy import func

# SYNTAX - func.coalesce(model.prop, default_value)
db.session.query(
    UserModel.id,
    func.coalesce(UserModel.active, false).label('is_active'),
).filter(
    UserModel.status == 'active'
).all()
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet