This can be done and it's not too complicated, but beware as in 95% of
the time it's a deficency in your model; ie. you can refactor your
model so that you don't need this.

If it's the other 5% of cases, here's what the code looks like (I
can't honestly remember if I read this in the docs or got it as an
answer from Mike someplace, so no link to source):

mapper(CompositeClass, join(ClassA, ClassB, join_condition <ie.
ClassB.a_id==ClassA.id>), properties={
    'composite_id': [table_A.c.id, table_B.c.a_id], #Be sure to mark
identical columns like this, otherwise inserts won't work
    'property_from_A: table_A.c.somecolumn,
    'property_from_B: [table_B.c.somecolumn,
table_B_superclass.c.somecolumn], #If B inherits from some class, this
needs to be specified explicitly
    'some_relation': relation(ClassD, primaryjoin=
(table_B.c.d_id==table_D.c.id))
    ...
    })

I hope the pseudocode isn't too confusing :-)


On Jan 16, 8:41 pm, justin potts <justinpotts...@gmail.com> wrote:
> Michael Bayer wrote:
> > On Jan 16, 2010, at 1:02 AM, justin potts wrote:
>
> >> I realize it's not the typical case, but I would like to know if it's
> >> possible to map a class to multiple tables at once, somewhat like
> >> mapping to a join statement. I've read the documentation about
> >> querying with joins, however this seems like a large amount of work to
> >> build an object sourced from multiple tables. Any help is appreciated.
>
> > what scheme for representing objects along multiple tables did you have in 
> > mind ?   this can mean any number of things.  
>
> I'm wondering if it's possible to instantiate an object with properties
> from different tables. Since it's usually a 1-1 mapping, I suppose the
> scheme in this case would be many-to-1.
-- 
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.


Reply via email to