diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2016-07-30 12:54:10 -0400 |
|---|---|---|
| committer | Gerrit Code Review <gerrit2@ln3.zzzcomputing.com> | 2016-07-30 12:54:10 -0400 |
| commit | cda9defe58f80759f2b40e658fd7b25eb4d92610 (patch) | |
| tree | 9182994c7b48648cf8267d3cd616e00410d91867 | |
| parent | 99248e843d4acde786e373a3056083ea34d72589 (diff) | |
| parent | 6327c59d4f34947128bd9b2860a1732a6932b4d7 (diff) | |
| download | sqlalchemy-cda9defe58f80759f2b40e658fd7b25eb4d92610.tar.gz | |
Merge "Index should extract __clause_element__() early"
| -rw-r--r-- | doc/build/changelog/changelog_11.rst | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/schema.py | 5 | ||||
| -rw-r--r-- | test/sql/test_metadata.py | 24 |
3 files changed, 37 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index cb95264cc..569373529 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -36,6 +36,16 @@ :meth:`.Query.order_by` has worked for a long time. Pull request courtesy Iuri Diniz. + .. change:: + :tags: bug, sql + :tickets: 3763 + + Fixed bug where :class:`.Index` would fail to extract columns from + compound SQL expressions if those SQL expressions were wrapped inside + of an ORM-style ``__clause_element__()`` construct. This bug + exists in 1.0.x as well, however in 1.1 is more noticeable as + hybrid_property @expression now returns a wrapped element. + .. changelog:: :version: 1.1.0b3 :released: July 26, 2016 diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 55d0b74e6..2c5daa17c 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2505,7 +2505,10 @@ class ColumnCollectionMixin(object): for expr in expressions: strname = None column = None - if not isinstance(expr, ClauseElement): + if hasattr(expr, '__clause_element__'): + expr = expr.__clause_element__() + + if not isinstance(expr, (ColumnElement, TextClause)): # this assumes a string strname = expr else: diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index d6e5a5dd4..cf7f7628a 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -2147,6 +2147,27 @@ class ConstraintTest(fixtures.TestBase): idx = Index('q', c) is_(idx.table, None) # lower-case-T table doesn't have indexes + def test_clauseelement_extraction_one(self): + t = Table('t', MetaData(), Column('x', Integer), Column('y', Integer)) + + class MyThing(object): + def __clause_element__(self): + return t.c.x + 5 + + idx = Index('foo', MyThing()) + self._assert_index_col_x(t, idx) + + def test_clauseelement_extraction_two(self): + t = Table('t', MetaData(), Column('x', Integer), Column('y', Integer)) + + class MyThing(object): + def __clause_element__(self): + return t.c.x + 5 + + idx = Index('bar', MyThing(), t.c.y) + + eq_(set(t.indexes), set([idx])) + def test_table_references(self): t1, t2, t3 = self._single_fixture() assert list(t2.c.a.foreign_keys)[0].references(t1) @@ -2911,8 +2932,9 @@ class ConstraintTest(fixtures.TestBase): def __clause_element__(self): return t2 - assert_raises( + assert_raises_message( exc.ArgumentError, + "Element Table\('t2', .* is not a string name or column element", Index, "foo", SomeClass() ) |
