Fix alias handling in has_index

This commit is contained in:
Konsta Vesterinen
2014-10-24 14:19:21 +03:00
parent 46db838ab2
commit f6ff024b80
3 changed files with 15 additions and 2 deletions

View File

@@ -9,6 +9,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
- Fix alias handling in has_index
0.27.4 (2014-10-23)

View File

@@ -159,12 +159,18 @@ def has_index(column):
has_index(table.c.locale) # False
has_index(table.c.id) # True
"""
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
)
return (
column is column.table.primary_key.columns.values()[0]
column is table.primary_key.columns.values()[0]
or
any(
index.columns.values()[0] is column
for index in column.table.indexes
for index in table.indexes
)
)

View File

@@ -1,4 +1,5 @@
import sqlalchemy as sa
from pytest import raises
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import has_index
@@ -23,6 +24,11 @@ class TestHasIndex(object):
self.table = ArticleTranslation.__table__
def test_column_that_belongs_to_an_alias(self):
alias = sa.orm.aliased(self.table)
with raises(TypeError):
assert has_index(alias.c.id)
def test_compound_primary_key(self):
assert has_index(self.table.c.id)
assert not has_index(self.table.c.locale)