set echo="debug" on your engine to see the rows coming back.  as is,  
the query from A returns:

  Row (u'aaa', None, None, None, u'a', 1)
  Row (u'aaa', u'ccc', u'bbb', None, u'c', 3)
  Row (u'aaa', u'ccc', u'bbb', None, u'd', 4)
  Row (u'aaa', None, u'bbb', None, u'b', 2)
  Row (u'aaa', None, u'bbb', None, u'c', 3)
  Row (u'aaa', None, u'bbb', None, u'd', 4)
  Row (u'aaa', u'ccc', u'bbb', u'ddd', u'd', 4)

changing the poly unions to look like this fixes it:

abcd_union = polymorphic_union(
     {
         'd':a_table.join(b_table).join(c_table).join(d_table),
         'c':a_table.join(b_table).join(c_table).select 
(a_table.c.type=="c"),
         'b':a_table.join(b_table).select(a_table.c.type=="b"),
         'a':a_table.select(a_table.c.type=='a')
     }, None)


someday ill be brave enough to build these poly unions into the core  
engine...they have come a long way in recent months but i still feel  
like we're still learning about their quirks (hence i force everyone  
to learn about them :)  ).


On Mar 7, 2007, at 3:01 PM, Mathieu Rouleau wrote:

> metadata = BoundMetaData('sqlite:///')
>
> class A(object):
>     def __init__(self, a):
>         self.a = a
>     def __repr__(self):
>         return '<A a=%s>' % self.a
>
> class B(A):
>     def __init__(self, a, b):
>         self.a = a
>         self.b = b
>     def __repr__(self):
>         return '<B a=%s b=%s>' % (self.a, self.b)
>
> class C(B):
>     def __init__(self, a, b, c):
>         self.a = a
>         self.b = b
>         self.c = c
>     def __repr__(self):
>         return '<C a=%s b=%s c=%s>' % (self.a, self.b, self.c)
>
> class D(C):
>     def __init__(self, a, b, c, d):
>         self.a = a
>         self.b = b
>         self.c = c
>         self.d = d
>     def __repr__(self):
>         return '<D a=%s b=%s c=%s d=%s>' % (self.a, self.b, self.c,
> self.d)
>
> a_table = Table('a_table', metadata,
>                 Column('id', Integer, primary_key=True),
>                 Column('a', String(32)),
>                 Column('type', String(30)))
>
> b_table = Table('b_table', metadata,
>                 Column('id', Integer, ForeignKey('a_table.id'),
> primary_key=True),
>                 Column('b', String(32)))
>
> c_table = Table('c_table', metadata,
>                 Column('id', Integer, ForeignKey('b_table.id'),
> primary_key=True),
>                 Column('c', String(32)))
>
> d_table = Table('d_table', metadata,
>                 Column('id', Integer, ForeignKey('c_table.id'),
> primary_key=True),
>                 Column('d', String(32)))
>
> abcd_union = polymorphic_union(
>     {
>         'd':a_table.join(b_table).join(c_table).join(d_table),
>         'c':a_table.join(b_table).join(c_table),
>         'b':a_table.join(b_table),
>         'a':a_table.select(a_table.c.type=='a')
>     }, None)
>
> bcd_union = polymorphic_union(
>     {
>         'd':a_table.join(b_table).join(c_table).join(d_table),
>         'c':a_table.join(b_table).join(c_table),
>         'b':a_table.join(b_table),
>     }, None)
>
> cd_union = polymorphic_union(
>     {
>         'd':a_table.join(b_table).join(c_table).join(d_table),
>         'c':a_table.join(b_table).join(c_table),
>     }, None)
>
> a_mapper = mapper(A, a_table, select_table=abcd_union,
> polymorphic_on=abcd_union.c.type, polymorphic_identity='a')
> b_mapper = mapper(B, b_table, select_table=bcd_union,
> inherits=a_mapper, polymorphic_on=bcd_union.c.type,
> polymorphic_identity='b')
> c_mapper = mapper(C, c_table, select_table=cd_union,
> inherits=b_mapper, polymorphic_on=cd_union.c.type,
> polymorphic_identity='c')
> mapper(D, d_table, inherits=c_mapper, polymorphic_identity='d')
>
> a_table.create()
> b_table.create()
> c_table.create()
> d_table.create()
>
> session = create_session()
> session.save(A(a='aaa'))
> session.save(B(a='aaa',b='bbb'))
> session.save(C(a='aaa',b='bbb',c='ccc'))
> session.save(D(a='aaa',b='bbb',c='ccc',d='ddd'))
> session.flush()
>
> #comment this clear for success
> session.clear()
>
> l = session.query(A).select()
> print l
>
> l = session.query(D).select()
> assert l[0].d == 'ddd'
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to