I created a stripped version of my application's model and I found the
problem. The problem is not in the polymorphism but int the inheritance
condition. I have cycles in my model so I had to use the
inherit_condition. I found that it is a big difference how I choose the
expression sides
inherit_condition=derived.c.id==base.c.id
or
inherit_condition=base.c.id==derived.c.id
But why? Is my inherit_condition incorrect at all?
The example code is attached.
Michael Bayer napsal(a):
>
> On Aug 25, 2008, at 1:05 PM, ml wrote:
>
>> One more thing :-)
>>
>> Now I have polymorphic_fetch="deferred" and in some needed cases I
>> call
>> query.with_polymorphic("*"). All works perfectly. But can I setup
>> something like with_polymorphic("*") on a mapper property? Because now
>> all relations to Base are polymorphic-deferred and it does me some
>> troubles.
>
> not really. What kind of problems ?
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
#!/usr/bin/python
# -*- coding: utf-8 -*-
from sqlalchemy import *
from sqlalchemy.orm import *
#engine = create_engine("postgres://localhost/test", echo=True)
engine = create_engine("sqlite://", echo=True)
metadata = MetaData()
base = Table("base", metadata,
Column("id", Integer, primary_key=True),
Column("id_parent", Integer, ForeignKey("base.id")),
Column("kind", Integer),
)
derived = Table("derived", metadata,
Column("id", Integer, ForeignKey("base.id"), primary_key=True),
Column("x", Integer),
Column("id_other", Integer),
ForeignKeyConstraint(["id_other"], ["base.id"],
"other_fg", use_alter=True),
)
class Base(object):
def __init__(self, id, parent):
self.id = id
self.parent = parent
class Derived(Base):
def __init__(self, id, parent):
Base.__init__(self, id, parent)
self.x = id
mapper(Base, base,
polymorphic_on=base.c.kind,
polymorphic_identity=0,
polymorphic_fetch="deferred",
properties = {
"children": relation(Base, cascade="all",
backref=backref("parent", remote_side=[base.c.id])),
}
)
mapper(Derived, derived,
inherits=Base,
#inherit_condition=base.c.id==derived.c.id,
inherit_condition=derived.c.id==base.c.id,
polymorphic_identity=1,
properties = {
"other": relation(Base, cascade="all", post_update=True,
primaryjoin=derived.c.id_other==base.c.id)
}
)
metadata.drop_all(engine)
metadata.create_all(engine)
Session = sessionmaker(bind=engine, autoflush=False, transactional=False)
session = Session()
#################################
# test data - chain 1<-2<-3
session.save(Derived(3, Derived(2, Derived(1, None))))
session.save(Derived(6, Derived(5, Derived(4, None))))
session.flush()
session.clear()
#################################
q = session.query(Base).with_polymorphic("*").get(3)
assert q.parent.parent.x == 1
q = session.query(Base).with_polymorphic("*").get(6)
assert q.parent.parent.x == 4, q.parent.parent.x