Below is a simplified case of a problem (bug?) I am running into.  I
have a polymorphic schema using joined-table inheritance.  In my OO
layer, a Group is an Agent, and a Lab is a Group.  In the DB layer,
Lab doesn't actually need to hold any extra information so it doesn't
need a separate table.

Instead, I would like to map the Lab object to the 'groups' table and
select only those types that are identified as a lab by the 'type'
discriminator column in 'agents' table.  I want,t hen, to map Lab to a
selectable.  However, when I try to create and flush a Lab instance
with the setup below, it is obvious that the attributes that should
map into the groups table aren't getting collected into the INSERT
statement correctly.

If I change `lab_select` to the simple join without the select
statement, the Lab instance is persisted correctly.  Of course, this
does nothing for the need to filter out non-lab entries.  Similarly,
if I create a useless extra table called `labs` and map to that
instead, the INSERT works

Any ideas on how to adjust the `lab_select` statement below so that
this works?

Thanks!
-Jason

----

import sqlalchemy as sa
from sqlalchemy import types as t
from sqlalchemy import orm

engine = sa.create_engine('sqlite:///:memory:', echo=True)
metadata = sa.MetaData()

agents = sa.Table('agents', metadata,
    sa.Column('id', t.Integer,
        sa.Sequence('agents_id_seq', optional=True),
primary_key=True),
    sa.Column('type', t.Unicode(50),
        sa.CheckConstraint("type IN ('agent', 'person', 'group',
'group_org', \
                'group_lab', 'group_contactlist')"), nullable=False),
    sa.Column('notes', t.UnicodeText),
)

groups = sa.Table('groups', metadata,
    sa.Column('id', t.Integer, unique=True),
    sa.Column('short_name', t.Unicode(50)),
    sa.Column('name', t.Unicode(255), nullable=False),
    sa.Column('group_status', t.Unicode(50),
        sa.CheckConstraint("group_status IN ('active', 'not active',
'closed')"),
            default=u'active', nullable=False),
    sa.Column('description', t.UnicodeText),
    sa.PrimaryKeyConstraint('id'),
    sa.ForeignKeyConstraint(['id'], ['agents.id'],
        onupdate="CASCADE", ondelete="CASCADE")
)


class Agent(object):
    pass

class Group(Agent):
    pass

class Lab(Group):
    pass


def map_agent_classes():
    orm.mapper(Agent, agents,
        polymorphic_on=agents.c.type,
        polymorphic_identity=u"agent",
        )

    orm.mapper(Group, groups,
        inherits=Agent,
        polymorphic_identity=u"group",
        )

    lab_select =
agents.outerjoin(groups).select(agents.c.type==u'group_lab',\
            use_labels=True).alias('labs')
    orm.mapper(Lab, lab_select,
        inherits=Group,
        polymorphic_identity=u"group_lab",
    )

map_agent_classes()

metadata.create_all(engine)
Session = orm.sessionmaker(bind=engine)

sess = Session()

lab = Lab()
lab.name = u"Lab Name"
lab.short_name = u"lab"

sess.add(lab)
sess.flush()

--------
IntegrityError: (IntegrityError) groups.name may not be NULL u'INSERT
INTO groups (id, short_name, name, group_status, description) VALUES
(?, ?, ?, ?, ?, ?)' [1, None, None, None, None]

--~--~---------~--~----~------------~-------~--~----~
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