python
Filter based on NULL Values in SQLAlchemy
NULL values filter on column values can be applied in SQLAlchemy using None keyword in filter query.
from sqlalchemy import or_
dbsession.query(
EmployeeModel
).filter(
or_(
EmployeeModel.status != 'active',
EmployeeModel.status == None #this is used to check NULL values
)
).all()
In the code snippet, we are checking whether the status of the employee is not active or none. We are getting records based on these conditions. We can also apply and_ operator in place of or_ id we want both the conditions needs to be true.
Was this helpful?
Similar Posts
- Replace NULL with false while Selecting in SQLAlchemy
- Filter query Sqlalchemy
- Using OR on filter query SQLAlchemy
- Apply join based on multiple conditions SQLAlchemy
- Using and on filter query SQLAlchemy
- Apply condition based multiple filters in SQLAlchemy query
- Filter by current time - n hours SQLAlchemy