diff --git a/CHANGES.rst b/CHANGES.rst index a73e10a..e9aaf2e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,7 @@ Here you can see the full list of changes between each SQLAlchemy-Utils release. ^^^^^^^^^^^^^^^^^^^ - Made assert_* functions automatically rollback session - +- Fix alias handling in has_unique_index 0.27.4 (2014-10-23) diff --git a/sqlalchemy_utils/functions/database.py b/sqlalchemy_utils/functions/database.py index a0b852a..aa4003e 100644 --- a/sqlalchemy_utils/functions/database.py +++ b/sqlalchemy_utils/functions/database.py @@ -198,8 +198,17 @@ def has_unique_index(column): has_unique_index(table.c.is_published) # True has_unique_index(table.c.is_deleted) # False has_unique_index(table.c.id) # True + + + :raises TypeError: if given column does not belong to a Table object """ - pks = column.table.primary_key.columns + table = column.table + if not isinstance(table, sa.Table): + raise TypeError( + 'Only columns belonging to Table objects are supported. Given ' + 'column belongs to %r.' % table + ) + pks = table.primary_key.columns return ( (column is pks.values()[0] and len(pks) == 1) or diff --git a/tests/functions/test_has_unique_index.py b/tests/functions/test_has_unique_index.py index f82f1a7..47ea1c4 100644 --- a/tests/functions/test_has_unique_index.py +++ b/tests/functions/test_has_unique_index.py @@ -1,10 +1,11 @@ +from pytest import raises import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base from sqlalchemy_utils import has_unique_index -class TestHasIndex(object): +class TestHasUniqueIndex(object): def setup_method(self, method): Base = declarative_base() @@ -31,6 +32,11 @@ class TestHasIndex(object): def test_primary_key(self): assert has_unique_index(self.articles.c.id) + def test_column_of_aliased_table(self): + alias = sa.orm.aliased(self.articles) + with raises(TypeError): + assert has_unique_index(alias.c.id) + def test_unique_index(self): assert has_unique_index(self.article_translations.c.is_deleted) diff --git a/tests/functions/test_make_order_by_deterministic.py b/tests/functions/test_make_order_by_deterministic.py index c40aff5..4c3c08e 100644 --- a/tests/functions/test_make_order_by_deterministic.py +++ b/tests/functions/test_make_order_by_deterministic.py @@ -17,7 +17,6 @@ class TestMakeOrderByDeterministic(TestCase): sa.func.lower(name) ) - class Article(self.Base): __tablename__ = 'article' id = sa.Column(sa.Integer, primary_key=True) @@ -31,6 +30,7 @@ class TestMakeOrderByDeterministic(TestCase): ) self.User = User + self.Article = Article def test_column_property(self): query = self.session.query(self.User).order_by(self.User.email_lower)