Mike Bayer avatar Mike Bayer committed dc4f0f1

- polymorphic_union() function respects the "key" of each
Column if they differ from the column's name.

Comments (0)

Files changed (4)

 =======
 CHANGES
 =======
+0.4.9
+=====
+- orm
+    - polymorphic_union() function respects the "key" of each 
+      Column if they differ from the column's name.
+
 0.4.8
 =====
 - orm
 
     - Added label() method to InstrumentedAttribute 
       to establish forwards compatibility with 0.5.
-
+     
 - sql
     - column.in_(someselect) can now be used as 
       a columns-clause expression without the subquery
-0.4.8
+0.4.9

lib/sqlalchemy/orm/util.py

 
         m = {}
         for c in table.c:
-            colnames.add(c.name)
-            m[c.name] = c
-            types[c.name] = c.type
+            colnames.add(c.key)
+            m[c.key] = c
+            types[c.key] = c.type
         colnamemaps[table] = m
 
     def col(name, table):

test/orm/inheritance/concrete.py

             assert set([repr(x) for x in c2.employees]) == set(["Engineer Kurt knows how to hack", "Manager Tom knows how to manage things"])
         self.assert_sql_count(testing.db, go, 1)
 
+class ColKeysTest(ORMTest):
+    def define_tables(self, metadata):
+        global offices_table, refugees_table
+        refugees_table = Table('refugee', metadata,
+           Column('refugee_fid', Integer, primary_key=True),
+           Column('refugee_name', Unicode(30), key='name'))
 
+        offices_table = Table('office', metadata,
+           Column('office_fid', Integer, primary_key=True),
+           Column('office_name', Unicode(30), key='name'))
+    
+    def insert_data(self):
+        refugees_table.insert().execute(
+            dict(refugee_fid=1, name=u"refugee1"),
+            dict(refugee_fid=2, name=u"refugee2")
+        )
+        offices_table.insert().execute(
+            dict(office_fid=1, name=u"office1"),
+            dict(office_fid=2, name=u"office2")
+        )
+        
+    def test_keys(self):
+        pjoin = polymorphic_union({
+           'refugee': refugees_table,
+           'office': offices_table
+        }, 'type', 'pjoin')
+        class Location(object):
+           pass
+
+        class Refugee(Location):
+           pass
+
+        class Office(Location):
+           pass
+
+        location_mapper = mapper(Location, pjoin, polymorphic_on=pjoin.c.type,
+                                polymorphic_identity='location')
+        office_mapper   = mapper(Office, offices_table, inherits=location_mapper,
+                                concrete=True, polymorphic_identity='office')
+        refugee_mapper  = mapper(Refugee, refugees_table, inherits=location_mapper,
+                                concrete=True, polymorphic_identity='refugee')
+
+        sess = create_session()
+        assert sess.query(Refugee).get(1).name == "refugee1"
+        assert sess.query(Refugee).get(2).name == "refugee2"
+
+        assert sess.query(Office).get(1).name == "office1"
+        assert sess.query(Office).get(2).name == "office2"
 
 if __name__ == '__main__':
     testenv.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.