From 671d7fdadfa4d795683d840c79780faa6b72df60 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 20 Jun 2007 21:59:38 +0000 Subject: [PATCH] added a unit test to test PK "collapsing" as in [changeset:2636], to help illustrate for [ticket:611] --- test/orm/inheritance/polymorph2.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/orm/inheritance/polymorph2.py b/test/orm/inheritance/polymorph2.py index 664fb78a38..65650f6686 100644 --- a/test/orm/inheritance/polymorph2.py +++ b/test/orm/inheritance/polymorph2.py @@ -867,9 +867,11 @@ class CustomPKTest(testbase.ORMTest): Column('id', Integer, primary_key=True), Column('type', String(30), nullable=False), Column('data', String(30))) + # note that the primary key column in t2 is named differently t2 = Table('t2', metadata, Column('t2id', Integer, ForeignKey('t1.id'), primary_key=True), Column('t2data', String(30))) + def test_custompk(self): """test that the primary_key attribute is propigated to the polymorphic mapper""" @@ -907,6 +909,48 @@ class CustomPKTest(testbase.ORMTest): ot1 = sess.query(T1).get(ot1.id) ot1.data = 'hi' sess.flush() + + def test_pk_collapses(self): + """test that a composite primary key attribute formed by a join is "collapsed" into its + minimal columns""" + + class T1(object):pass + class T2(T1):pass + + # create a polymorphic union with the select against the base table first. + # with the join being second, the alias of the union will + # pick up two "primary key" columns. technically the alias should have a + # 2-col pk in any case but the leading select has a NULL for the "t2id" column + d = util.OrderedDict() + d['t1'] = t1.select(t1.c.type=='t1') + d['t2'] = t1.join(t2) + pjoin = polymorphic_union(d, None, 'pjoin') + + #print pjoin.original.primary_key + #print pjoin.primary_key + assert len(pjoin.primary_key) == 2 + + mapper(T1, t1, polymorphic_on=t1.c.type, polymorphic_identity='t1', select_table=pjoin) + mapper(T2, t2, inherits=T1, polymorphic_identity='t2') + assert len(class_mapper(T1).primary_key) == 1 + assert len(class_mapper(T1).get_select_mapper().compile().primary_key) == 1 + + print [str(c) for c in class_mapper(T1).primary_key] + ot1 = T1() + ot2 = T2() + sess = create_session() + sess.save(ot1) + sess.save(ot2) + sess.flush() + sess.clear() + + # query using get(), using only one value. this requires the select_table mapper + # has the same single-col primary key. + assert sess.query(T1).get(ot1.id).id is ot1.id + + ot1 = sess.query(T1).get(ot1.id) + ot1.data = 'hi' + sess.flush() if __name__ == "__main__": testbase.main() -- 2.47.3