From f9e12b99a9aef6680353d65feac3201125079e38 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Mon, 20 Oct 2014 12:09:56 +0300 Subject: [PATCH] Add support for InstrumentedAttributes in get_mapper --- sqlalchemy_utils/functions/orm.py | 2 ++ tests/functions/test_get_mapper.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/sqlalchemy_utils/functions/orm.py b/sqlalchemy_utils/functions/orm.py index 9ff3a56..4ef8b47 100644 --- a/sqlalchemy_utils/functions/orm.py +++ b/sqlalchemy_utils/functions/orm.py @@ -83,6 +83,8 @@ def get_mapper(mixed): mixed = mixed.element if isinstance(mixed, AliasedInsp): return mixed.mapper + if isinstance(mixed, sa.orm.attributes.InstrumentedAttribute): + mixed = mixed.class_ if isinstance(mixed, sa.Table): mappers = [ mapper for mapper in mapperlib._mapper_registry diff --git a/tests/functions/test_get_mapper.py b/tests/functions/test_get_mapper.py index 7f75620..a282be9 100644 --- a/tests/functions/test_get_mapper.py +++ b/tests/functions/test_get_mapper.py @@ -42,6 +42,11 @@ class TestGetMapper(object): sa.inspect(self.Building) ) + def test_instrumented_attribute(self): + assert ( + get_mapper(self.Building.id) == sa.inspect(self.Building) + ) + def test_table_alias(self): alias = sa.orm.aliased(self.Building.__table__) assert (