6

So i wanna use a for loop in a query and pull results if a record's field is equal to an object's property (in a list of objects) how do i do that? This is my code:

you = session.query(Users).filter_by(id=login_session['userid']).first()

friends = session.query(Friends).filter_by(user_id=login_session['userid']).all()

dashboard = session.query(Markers).filter(Markers.owner == f.friend_id for f in friends).all()

but then i get this:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pearadox6/travellr/app.py", line 423, in feed
    dashboard = session.query(Markers).filter(Markers.owner == f.friend_id for f in friends).all()
  File "<string>", line 1, in <lambda>
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 51, in generate
    fn(self, *args[1:], **kw)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 1216, in filter
    criterion = expression._literal_as_text(criterion)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/expression.py", line 1521, in _literal_as_text
    "SQL expression object or string expected."
ArgumentError: SQL expression object or string expected.

Why?

1 Answer 1

8

Why?

Because you can't pass a generator object as an argument for filter:

session.query(Markers).filter(Markers.owner == f.friend_id for f in friends).all()

Use in_ with a list instead:

session.query(Markers).filter(Markers.owner.in_([f.friend_id for f in friends)]).all()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.