Please have a look at the attached testcase that mixes single-table polymorphic inheritance, primaryjoin overides on the relation() and a secondary table.
SA seems to add a WHERE clause to filter for the polymorphic type on relations that it calculates, but forgets to add it for the test case. Is this supposed to work, or is it necessary to specify the filter in the relation()? Thx, Rick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
from sqlalchemy import * from sqlalchemy.orm import * #engine = create_engine('sqlite:///:memory:', echo=True) engine = create_engine('sqlite:///:memory:') meta = MetaData(bind=engine) secent = Table('secent', meta, Column('id', INT, nullable=False, primary_key=True), Column('typ', VARCHAR(8), nullable=False), Column('nm', VARCHAR(20)) ) secrel = Table('secrel', meta, Column('idparent', INT, ForeignKey('secent.id'), primary_key=True), Column('idchild', INT, ForeignKey('secent.id'), primary_key=True) ) meta.create_all() class K(object): def __init__(self, **kw): for k,v in kw.items(): setattr(self, k,v) class Secent(K): pass class SecUser(Secent): pass class SecRole(Secent): pass class SecFilter(Secent): pass class SecGroup(Secent): pass m_secent = mapper(Secent, secent, polymorphic_on = secent.c.typ, polymorphic_identity = '?', properties = {'roles': relation(SecRole, primaryjoin = secrel.c.idparent == secent.c.id, secondaryjoin = secrel.c.idchild == secent.c.id, secondary = secrel), 'filters': relation(SecFilter, primaryjoin = secrel.c.idparent == secent.c.id, secondaryjoin = secrel.c.idchild == secent.c.id, secondary = secrel), 'groups': relation(SecGroup, secondary=secrel, primaryjoin = secrel.c.idchild == secent.c.id, secondaryjoin = secrel.c.idparent == secent.c.id) } ) mapper(SecUser, secent, inherits = m_secent, polymorphic_identity = 'U') mapper(SecRole, secent, inherits = m_secent, polymorphic_identity = 'R') mapper(SecFilter,secent, inherits = m_secent, polymorphic_identity = 'F') mapper(SecGroup, secent, inherits = m_secent, polymorphic_identity = 'G', properties = {'members': relation(SecUser, secondary=secrel, primaryjoin = secrel.c.idparent == secent.c.id, secondaryjoin = secrel.c.idchild == secent.c.id), 'subgroups': relation(SecGroup, secondary=secrel, primaryjoin = secrel.c.idparent == secent.c.id, secondaryjoin = secrel.c.idchild == secent.c.id, backref='parent_groups') } ) S = create_session() u1 = SecUser(nm='user1') u2 = SecUser(nm='user2') u3 = SecUser(nm='user3') map(S.save, (u1,u2,u3)) r1 = SecRole(nm='r1') r2 = SecRole(nm='r2') r3 = SecRole(nm='r3') map(S.save, (r1,r2,r3)) f1 = SecFilter(nm='f1') f2 = SecFilter(nm='f2') f3 = SecFilter(nm='f3') map(S.save, (f1,f2,f3)) g1 = SecGroup(nm='g1') g2 = SecGroup(nm='g2') g3 = SecGroup(nm='g3') map(S.save, (g1,g2,g3)) u1.roles = [r1, r2] u2.roles = [r1, r3] u3.roles = [r3] u1.filters = [f2, f3] u2.filters = [f2, f3] u3.filters = [f1] u1.groups = [g1, g2, g3] u2.groups = [g1, g2] u3.groups = [g2, g3] S.flush() S.clear() u = S.query(SecUser).filter(SecUser.nm == 'user1').first() assert len(u.roles) == 2 assert len(u.filters) == 2 assert len(u.groups) == 3