Thanks for that... I didn't manage to get it to work straight off, but it seems like I'm close.
Tried a couple things just to force it to behave as I wanted. # defined the selectable selectable = select( [ sql.column( 'x' ), sql.column('y')], from_obj = [ func.foo_props(bindparam( 'x' )) ] , bind=... ).alias( 't') # mapping mapper( FooProps , selectable , primary_key = [ selectable.c.x ] ) this fails with: InvalidRequestError: Could not find any Table objects in mapped table 'SELECT x,y FROM foo_props(%(param_1)s)' #in sqlalchemy.orm.mapper the find_tables() call defaults to include_aliases = False, forced it to be true: def _configure_pks(self): self.tables = sqlutil.find_tables(self.mapped_table,include_aliases=True) (include_selectable = True worked also) then define the relationship: mapper( Foo , foo_table, properties = dict( props = relation( FooProps, primaryjoin = foo_table.c.x = selectable.c.x, foreignkey = [ selectable.c.x ] ))) So now when I invoke the relationship a.foo there a call: SELECT t.x, t.y FROM (SELECT x,y FROM foo_props(%(x)s)) AS t WHERE %(param_1)s = t.x but only the param_1 is defined in the parameters to the call. I can hack it by just setting the initial bindparam('x') to be bindparam('param1'), but that's not really ideal obviously SELECT t.x, t.y FROM (SELECT x,y FROM foo_props(%(param_1)s)) AS t WHERE %(param_1)s = t.x so what I'm wondering is how to get that 'x' value taken from my instance a (a.x, in other words) and supplied as a parameter to the query in a safer way. eg. relationship( ..., extra_params( x=Foo.c.x ) ) Or from the other direction maybe a way of specifying the bindparam key for the relationship foreignkey value itself ( and then specify it to match the bindparam argument ). eg. relationship( ..., foreign_keys = dict( x= selectable.c.x )) though not sure how that'd work for compound fks. Probably just missing something obvious... -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.