From 17adfc8adce3f19fc43aede00057b33c6b85c26e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 21 Oct 2008 21:07:04 +0000 Subject: [PATCH] - polymorphic_union() function respects the "key" of each Column if they differ from the column's name. --- CHANGES | 5 +++- lib/sqlalchemy/orm/util.py | 6 ++-- test/orm/inheritance/concrete.py | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index afc5209db0..fd8a71b91b 100644 --- a/CHANGES +++ b/CHANGES @@ -21,7 +21,10 @@ CHANGES - relation() won't hide unrelated ForeignKey errors inside of the "please specify primaryjoin" message when determining join condition. - + + - polymorphic_union() function respects the "key" of each + Column if they differ from the column's name. + - sql - Further simplified SELECT compilation and its relationship to result row processing. diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index f461c4bdcb..4172b99cc1 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -92,9 +92,9 @@ def polymorphic_union(table_map, typecolname, aliasname='p_union'): 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): diff --git a/test/orm/inheritance/concrete.py b/test/orm/inheritance/concrete.py index 45a082fdf7..1117d53c91 100644 --- a/test/orm/inheritance/concrete.py +++ b/test/orm/inheritance/concrete.py @@ -207,7 +207,54 @@ class ConcreteTest(ORMTest): 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, key='name')) + + offices_table = Table('office', metadata, + Column('office_fid', Integer, primary_key=True), + Column('office_name', Unicode, 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() -- 2.47.3