]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added a unit test to test PK "collapsing" as in [changeset:2636], to help illustrate...
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Jun 2007 21:59:38 +0000 (21:59 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Jun 2007 21:59:38 +0000 (21:59 +0000)
test/orm/inheritance/polymorph2.py

index 664fb78a38c51adaf427d3169aa1d4f8b1e5883c..65650f6686f36197c77985a8a2d259c556e5bbfa 100644 (file)
@@ -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()