this kind of pattern is usually handled by column_property(), assuming you can correlate your subquery to the parent within the WHERE clause such that the return value is a scalar.
Below if you really just wanted "the first row" you'd want to say "limit(1)". If you really want that exact same first row of the table every time, it would be better as just an additional method or descriptor on your OrderChain object, (or even if not) perhaps even as a class method if the value truly has no correlation to the identity of the parent (which seems unlikely). Two different strategies at: http://www.sqlalchemy.org/docs/orm/mapper_config.html#sql-expressions-as-mapped-attributes http://www.sqlalchemy.org/docs/orm/relationships.html#building-query-enabled-properties - specific to relationship(), but the same idea could be applied to columns too. On Mar 16, 2011, at 10:23 AM, hoesley wrote: > I am trying to create a property on a mapped class which basically > executes a subquery. Here is what I'm trying to do in non-working > pseudo code: > > mapper(OrderChain, Table('order_chains', self.meta, > autoload=True), > properties={'entries': relation(OrderChainEntry, > order_by=Column('timestamp')), > 'first_cancel': > select(OrderChainEntry).filter('type'='new').first() > }) > > Is something like this possible? Or what's the best way to approach > it? > > Also, if I've mangled terminology and/or written something that makes > no sense, please let me know - I'm very new to this package! > > Cheers, > Andrew > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
