Fix has_index, fixes #148

- Make has_index work with tables without primary keys
This commit is contained in:
Konsta Vesterinen
2015-06-04 20:26:57 +03:00
parent afe4db7bbc
commit 0cd297c4fc
3 changed files with 19 additions and 1 deletions

View File

@@ -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)
^^^^^^^^^^^^^^^^^^^

View File

@@ -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

View File

@@ -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)