diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-23 18:38:48 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-24 09:30:57 -0400 |
commit | 33616a85c7c35ec1b1756a43e44a621b744e75fa (patch) | |
tree | f3309a93aed55d77e29313d6404b9bbd75c52774 /lib/sqlalchemy/sql/schema.py | |
parent | 8f5b65f4316e21d19b00266cdd9eded3d4ec51b2 (diff) | |
download | sqlalchemy-33616a85c7c35ec1b1756a43e44a621b744e75fa.tar.gz |
Ensure all Index arguments are counted when matching expr/columns
Fixed issue where :class:`.Index` object which contained a mixture of
functional expressions which were not resolvable to a particular column,
in combination with string-based column names, would fail to initialize
its internal state correctly leading to failures during DDL compilation.
Fixes: #4778
Change-Id: I0fa9c627a1fde92ba8b9ed10af167c156012bd5d
Diffstat (limited to 'lib/sqlalchemy/sql/schema.py')
-rw-r--r-- | lib/sqlalchemy/sql/schema.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index c84e3ee21..c63a3160f 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2738,7 +2738,9 @@ def _to_schema_column(element): def _to_schema_column_or_string(element): - if hasattr(element, "__clause_element__"): + if element is None: + return element + elif hasattr(element, "__clause_element__"): element = element.__clause_element__() if not isinstance(element, util.string_types + (ColumnElement,)): msg = "Element %r is not a string name or column element" @@ -2816,11 +2818,16 @@ class ColumnCollectionMixin(object): ) ) + def _col_expressions(self, table): + return [ + table.c[col] if isinstance(col, util.string_types) else col + for col in self._pending_colargs + ] + def _set_parent(self, table): - for col in self._pending_colargs: - if isinstance(col, util.string_types): - col = table.c[col] - self.columns.add(col) + for col in self._col_expressions(table): + if col is not None: + self.columns.add(col) class ColumnCollectionConstraint(ColumnCollectionMixin, Constraint): @@ -3616,8 +3623,7 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): ) in coercions.expect_col_expression_collection( roles.DDLConstraintColumnRole, expressions ): - if add_element is not None: - columns.append(add_element) + columns.append(add_element) processed_expressions.append(expr) self.expressions = processed_expressions @@ -3655,11 +3661,12 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): self.table = table table.indexes.add(self) + expressions = self.expressions + col_expressions = self._col_expressions(table) + assert len(expressions) == len(col_expressions) self.expressions = [ expr if isinstance(expr, ClauseElement) else colexpr - for expr, colexpr in util.zip_longest( - self.expressions, self.columns - ) + for expr, colexpr in zip(expressions, col_expressions) ] @property |