On 02/23/2016 06:47 AM, gio wrote:
Hi thanks for your answer Without filter i did not expect any big difference between query(Keyword) or a hypothetical query(User.keywords) but I was thinking it will be nice to do something like this session.query(User.keywords).filter(User ==4) without joins.. My goal was to have a simple query that return a structure like the following with user and user.keywords from sqlalchemy import func myquery = session.query(UserKeyword.user_id, func.array_agg(UserKeyword.keyword_id)).group_by(UserKeyword.user_id).all() myquery result [(4, [3, 4]), (3, [1, 2])] I was thinking associationproxy could help to simplify the ORM statement but it seems I have to join
OK well the concept of query(Class.somerelationship) as loading that related entity is a long standing issue we have in bitbucket. It's strictly a "nice to have" so has not been a priority for some time.
In this case, we're talking about the associationproxy, which means that we'd also extend that behavior to associationproxy, OK. However, if someone queried for query(Class.someproxy), again anything built into SQLAlchemy would at best be query(RelatedClass). What you have there is a specific series of columns and transformations, e.g. the user_id + array_agg, which also is database specific too (only works on PG). You could in theory subclass associationproxy to provide a __clause_element__() which delivers this part, but it wouldn't intrinsically do the group_by() part.
The most succinct thing that's easy to do would be if you built a with_transformation() transformer, so that you could get all the bits you need done in one step. see http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html?highlight=with_transformation#sqlalchemy.orm.query.Query.with_transformation for details on that.
Regards g Am Montag, 22. Februar 2016 12:51:55 UTC+1 schrieb gio: Hi I was trying to use the associationproxy feature like explained in the basic example http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html <http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html> Now my question is: Is it possible to do a query like and have as result an array of array of keywords? session.query(User.keywords) Regards g -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
-- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.