Noor
Noor

Reputation: 20178

SQLAlchemy Error when filtering

I'm using using SQLAlchemy to fiter but getting an error:

user = session.query.filter(User.id == 99).one()


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'filter'

Does someone know how to filter because on SQLAlchemy Page, I saw this:

query = session.query(User).filter

Upvotes: 2

Views: 4953

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124928

query is a function, you need to pass in the User class to call it:

user = session.query(User).filter(User.id == 99).one()
                    ^^^^^^

SQLAlchemy cannot divine from the filter alone what type of object you want returned otherwise.

Upvotes: 5

Related Questions