can i... maybe something like
table_A = Table( 'A', meta,
Column( 'name', String(length=200, ), ),
Column( 'db_id', primary_key= True, type_= Integer, ),
)
table_B = Table( 'B', meta,
Column( 'dataB', String(length=200 ),
Column( 'db_id', primary_key= True, type_= Integer, ),
)
table_C = Table( 'C', meta,
Column( 'name', String(length=200 ),
Column( 'dataC', String(length=200 ),
Column( 'db_id', primary_key= True, type_= Integer, ),
)
meta.create_all()
class A( Base):
props = ['db_id', 'name']
class B( Base):
props = ['db_id', 'dataB']
class C( A):
props = ['db_id', 'name', 'dataC']
pu_a = polymorphic_union( {
'A': table_A,
'C': table_C,
}, 'atype', 'pu_a', ) #concrete table
mapper_A = mapper( A, table_A,
polymorphic_identity= 'A',
polymorphic_on= pu_a.c.atype,
with_polymorphic= ('*', pu_a.alias( 'pu_a' )),
)
mapper_B = mapper( B, table_B,
with_polymorphic= None,
)
mapper_C = mapper( C, table_C,
concrete= True,
inherits= mapper_A,
polymorphic_identity= 'C',
with_polymorphic= None,
)
#populate
a = A()
b = B()
c = C()
a.name = 'anna'
b.dataB = 'gun'
c.name = 'cc'
c.dataC = 'mc'
session = create_session()
session.save(a)
session.save(b)
session.save(c)
session.flush()
def query_by_id( session, klas, idname, oid, ):
#single
if config.session_clear: session.clear()
q = session.query( klas).filter_by( **{idname: oid})
#TODO: filter by atype for concrete?
m = class_mapper( klas)
concrete = bool( m.with_polymorphic[1] )
print 111111111111111111111111
print m.polymorphic_on
print m.with_polymorphic[1].c.atype
if concrete: q= q.filter( m.polymorphic_on ==
klas.__name__)
#if concrete: q= q.filter( m.with_polymorphic[1].c.atype
== klas.__name__)
return q.all()
print query_by_id( session, A, 'db_id', 1, str(a) )
filtering by m.with_polymorphic[1].c.atype is ok (commented).
i guess i see the error:
polymorphic_on is on pu_a,
with_polymorphic is on pu_a.alias()...
but u have some autoaliasing in there anyway, no?
btw, have u planned to get relations to polymorphic concretes to work
somewhen ? eventualy query.get(id) to work? i.e. identity to include
the type
On Wednesday 03 December 2008 22:21:12 Michael Bayer wrote:
> thats strange, i dont suppose you could send me how you're setting
> up that polymorphic union
>
> On Dec 3, 2008, at 3:05 PM, [EMAIL PROTECTED] wrote:
> > and maybe related:
> > def query_by_id( klas, idname, oid, .....):
> > q = session.query( klas).filter_by( **{idname: oid})
> > # filter by atype for concrete
> > m = class_mapper( klas)
> > concrete = bool( m.with_polymorphic[1] )
> > if concrete: q= q.filter( m.polymorphic_on == klas.__name__)
> > return q.all()
> >
> > generates a query like
> >
> > SELECT pu_a.*
> > FROM (SELECT A.* FROM "A"
> > UNION ALL
> > SELECT C.* FROM "C")
> > AS pu_a,
> > (SELECT A.* FROM "A"
> > UNION ALL
> > SELECT C.* FROM "C")
> > AS pu_a
> > WHERE pu_a.db_id = ? AND pu_a.atype = ?
> >
> > with repeated union twice in the FROM, and error thereof.
> > Somehow the mapper.polymorphic_on is not recognized as already
> > being there and pulls the union once more?
> > if there's no q.filter( m.polymorphic_on == klas.__name__), no
> > errors (but returns multiple objects).
> >
> > this on latest trunk.
> >
> > svil
> >
> > On Wednesday 03 December 2008 19:29:31 Michael Bayer wrote:
> >> it needed some more work. the final version of this fix is in
> >> r5412.
> >>
> >> On Dec 3, 2008, at 10:49 AM, Gaetan de Menten wrote:
> >>> On Wed, Dec 3, 2008 at 16:04, Michael Bayer
> >>>
> >>> <[EMAIL PROTECTED]> wrote:
> >>>> this ones big, i can handle it. the attached patch makes
> >>>> your case work, but the problem represented here still makes
> >>>> itself apparent in other ways and I havent strength tested
> >>>> this patch. you might want to see if this patch works in all
> >>>> of your test cases.
> >>>
> >>> FWIW, my (only) "more complete" test involving polymorphic
> >>> concrete inheritance passes too (and produces correct SQL).
> >>> Your patch seem fine and doesn't seem to break unrelated tests.
> >>>
> >>> Thanks for the quick reaction time, as usual.
> >>> --
> >>> Gaƫtan de Menten
> >>> http://openhex.org
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---