diff --git a/CHANGES.rst b/CHANGES.rst index 285de01..bc094fc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changelog Here you can see the full list of changes between each SQLAlchemy-Utils release. +0.30.8 (2015-06-xx) +^^^^^^^^^^^^^^^^^^^ + +- Make has_index work with tables without primary keys (#148) + + 0.30.7 (2015-05-28) ^^^^^^^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/functions/database.py b/sqlalchemy_utils/functions/database.py index 3e7b034..040f240 100644 --- a/sqlalchemy_utils/functions/database.py +++ b/sqlalchemy_utils/functions/database.py @@ -247,8 +247,9 @@ def has_index(column): 'Only columns belonging to Table objects are supported. Given ' 'column belongs to %r.' % table ) + primary_keys = table.primary_key.columns.values() return ( - column is table.primary_key.columns.values()[0] + (primary_keys and column is primary_keys[0]) or any( index.columns.values()[0] is column diff --git a/tests/functions/test_has_index.py b/tests/functions/test_has_index.py index 3dfabf1..9c0e1c6 100644 --- a/tests/functions/test_has_index.py +++ b/tests/functions/test_has_index.py @@ -39,3 +39,14 @@ class TestHasIndex(object): def test_compound_column_index(self): assert has_index(self.table.c.is_deleted) assert not has_index(self.table.c.is_archived) + + def test_table_without_primary_key(self): + Base = declarative_base() + + article = sa.Table( + 'article', + sa.MetaData(), + sa.Column('name', sa.String) + ) + + assert not has_index(article.c.name)