Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using for loops in sqlalchemy query

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?

like image 374
ryanwaite28 Avatar asked Aug 16 '16 13:08

ryanwaite28


People also ask

How does the querying work with SQLAlchemy?

All SELECT statements generated by SQLAlchemy ORM are constructed by Query object. It provides a generative interface, hence successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.

Is SQLAlchemy worth learning?

SQLAlchemy is the ORM of choice for working with relational databases in python. The reason why SQLAlchemy is so popular is because it is very simple to implement, helps you develop your code quicker and doesn't require knowledge of SQL to get started.

What is subquery in SQLAlchemy?

The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.

What does all () do in SQLAlchemy?

all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present. This does not apply to a query that is against individual columns.


1 Answers

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()
like image 53
DeepSpace Avatar answered Sep 20 '22 21:09

DeepSpace