Fix alias handling in has_unique_index

This commit is contained in:
Konsta Vesterinen
2014-10-24 14:15:54 +03:00
parent ee94d5c976
commit 46db838ab2
4 changed files with 19 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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