On Aug 10, 2010, at 11:20 PM, botz wrote:
> oops, didn't mean to submit that quite yet. crazy dog...
>
> anyways, given that I have foo_props() which acts as a kind of from-
> clause
> as in
> select * from foo_props( 123);
>
> I'd like to use that in relationship to a Foo class
>
> i.e.
> mapper( Foo, foo_table, properties( props = relationship( FooProps ) )
>
> but of course I can't do a join between Foo and foo_props on foo_id
> since foo_props takes it as a parameter.
>
> I can do a
> query( FooPropsType ).from_statement( "select * from foo_props( %d )"
> % foo_id )
> but I'm not sure how to set that up as a relationship or
> column_property.
>
> Any hints would be greatly appreciated.
so the prerequisites for relationship() are:
- the target is a mapped class
- there's some way to "join" from parent to child
easy enough, the prerequisites for mapped class are:
- there's a "selectable", meaning it has named columns and can be
SELECTed from
- the "selectable" has one or more columns that define a primary key,
either naturally or artificially
so with a custom function you'd start with the example at
http://www.sqlalchemy.org/docs/sqlexpression.html#functions , its maybe 5 slots
down where you use column(), select(), and from_obj to set up a selectable.
then you map to it, with mapper() (or declarative.__table__ if you're going
that route). The mapper() will require the "primary_key" argument, such as
primary_key=[myselectable.c.foo].
since your selectable also has no "foreign keys" as part of its metadata, the
relationship() needs to have that too plus a join condition:
relationship(FooProps, primaryjoin=foo_table.c.x==selectable.c.y,
foreign_keys=[selectable.c.y])
and thats it
>
> --
> 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.