dependency processing glitch with inheritance

Issue #360 resolved
Mike Bayer repo owner created an issue

note theres no backrefs, placing them makes the problem go away. also un-inheriting Child2 makes the problem go away. which means the issue involves the polymorphic dependency processing in unit of work. ugh.

from sqlalchemy import *

meta = BoundMetaData("sqlite://")
meta.engine.echo = True

parent = Table("parent", meta,
    Column("id", Integer, primary_key=True),
    Column("parent_data", String(50)),
    Column("type", String(10))
    )

child1 = Table("child1", meta,
    Column("id", Integer, ForeignKey("parent.id"), primary_key=True),
    Column("child1_data", String(50))
    )

child2 = Table("child2", meta,
    Column("id", Integer, ForeignKey("parent.id"), primary_key=True),
    Column("child1_id", Integer, ForeignKey("child1.id"),
nullable=False),
    Column("child2_data", String(50))
    )

meta.create_all()

parent_join = polymorphic_union(
        {
                'child1': parent.join(child1),
                'child2': parent.join(child2),
                'parent': parent.select(parent.c.type=='parent')
        }, None, 'pjoin')

class Parent(object):
        pass

Parent.mapper = mapper(Parent, parent, select_table=parent_join,
        polymorphic_on=parent.c.type, polymorphic_identity='parent')

class Child1(Parent):
        pass

Child1.mapper = mapper(Child1, child1, inherits=Parent.mapper,
        polymorphic_identity='child1')

class Child2(Parent):
        pass

Child2.mapper = mapper(Child2, child2, properties={
                "child1": relation(Child1,
                        primaryjoin=child2.c.child1_id==child1.c.id
                )
        },
        inherits=Parent.mapper,
        polymorphic_identity='child2')

session = create_session(bind_to=meta.engine)

c1 = Child1()
c1.child1_data = "qwerty"
session.save(c1)
session.flush()

session.clear()

c1 = session.query(Child1).get_by(child1_data="qwerty")
c2 = Child2()
c2.child1 = c1
c2.child2_data = "asdfgh"
session.save(c2)
session.flush()

Comments (2)

  1. Mike Bayer reporter

    changeset:2077, includes two unit tests. the issue was a cyclical dependency sort in conjunction with many-to-one issue and can be tested without using inheritance (see the changeset for examples)

  2. Log in to comment