1

I have some code:

def search(name: str = None):
    q = db.query(City).join(Street)

    if name:
        q.filter(City.name==name)

    return q.all()

But it doesn't add a condition to my query and returns all the results. How in SQLAlchemy can I split the query into conditions?

jatkso
  • 41
  • 5

1 Answers1

0

you are not adding it to the q. you have to assign it back to it, so, replace the your filter with:

q = q.filter(City.name==name)
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59