fix sqlalchemy query