From 208137e5d652b1c2ad8d821e2c803cad924f24cc Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Tue, 9 Sep 2014 08:57:10 +0300 Subject: [PATCH] Fix ambiguous column sorting --- sqlalchemy_utils/functions/orm.py | 7 ++++--- tests/test_sort_query.py | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/sqlalchemy_utils/functions/orm.py b/sqlalchemy_utils/functions/orm.py index fddbf56..831069b 100644 --- a/sqlalchemy_utils/functions/orm.py +++ b/sqlalchemy_utils/functions/orm.py @@ -508,10 +508,11 @@ def get_all_descriptors(expr): insp = sa.inspect(expr) polymorphic_mappers = get_polymorphic_mappers(insp) if polymorphic_mappers: - - attrs = {} + attrs = dict(get_mapper(expr).all_orm_descriptors) for submapper in polymorphic_mappers: - attrs.update(submapper.all_orm_descriptors) + for key, descriptor in submapper.all_orm_descriptors.items(): + if key not in attrs: + attrs[key] = descriptor return attrs return get_mapper(expr).all_orm_descriptors diff --git a/tests/test_sort_query.py b/tests/test_sort_query.py index 1b8154a..d495d2e 100644 --- a/tests/test_sort_query.py +++ b/tests/test_sort_query.py @@ -273,6 +273,13 @@ class TestSortQueryWithPolymorphicInheritance(TestCase): ) assert_contains('ORDER BY article.category ASC', query) + def test_with_ambiguous_column(self): + query = sort_query( + self.session.query(self.TextItem), + 'id' + ) + assert_contains('ORDER BY text_item.id ASC', query) + class TestSortQueryWithCustomPolymorphic(TestCase): """