diff options
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 12 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/elements.py | 37 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/naming.py | 2 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/schema.py | 7 |
4 files changed, 39 insertions, 19 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index c6c30629d..5ecec7d6c 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -3022,6 +3022,10 @@ class DDLCompiler(Compiled): text = "CREATE " if index.unique: text += "UNIQUE " + if index.name is None: + raise exc.CompileError( + "CREATE INDEX requires that the index have a name" + ) text += "INDEX %s ON %s (%s)" % ( self._prepared_index_name(index, include_schema=include_schema), preparer.format_table( @@ -3038,6 +3042,11 @@ class DDLCompiler(Compiled): def visit_drop_index(self, drop): index = drop.element + + if index.name is None: + raise exc.CompileError( + "DROP INDEX requires that the index have a name" + ) return "\nDROP INDEX " + self._prepared_index_name( index, include_schema=True ) @@ -3251,7 +3260,8 @@ class DDLCompiler(Compiled): text = "" if constraint.name is not None: formatted_name = self.preparer.format_constraint(constraint) - text += "CONSTRAINT %s " % formatted_name + if formatted_name is not None: + text += "CONSTRAINT %s " % formatted_name text += "UNIQUE (%s)" % ( ", ".join(self.preparer.quote(c.name) for c in constraint) ) diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index b6462b334..78c434cff 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -4263,7 +4263,11 @@ class ColumnClause(roles.LabeledColumnExprRole, Immutable, ColumnElement): def _render_label_in_columns_clause(self): return self.table is not None - def _gen_label(self, name): + @property + def _ddl_label(self): + return self._gen_label(self.name, dedupe_on_key=False) + + def _gen_label(self, name, dedupe_on_key=True): t = self.table if self.is_literal: @@ -4287,21 +4291,22 @@ class ColumnClause(roles.LabeledColumnExprRole, Immutable, ColumnElement): assert not isinstance(label, quoted_name) label = quoted_name(label, t.name.quote) - # ensure the label name doesn't conflict with that - # of an existing column. note that this implies that any - # Column must **not** set up its _label before its parent table - # has all of its other Column objects set up. There are several - # tables in the test suite which will fail otherwise; example: - # table "owner" has columns "name" and "owner_name". Therefore - # column owner.name cannot use the label "owner_name", it has - # to be "owner_name_1". - if label in t.c: - _label = label - counter = 1 - while _label in t.c: - _label = label + "_" + str(counter) - counter += 1 - label = _label + if dedupe_on_key: + # ensure the label name doesn't conflict with that of an + # existing column. note that this implies that any Column + # must **not** set up its _label before its parent table has + # all of its other Column objects set up. There are several + # tables in the test suite which will fail otherwise; example: + # table "owner" has columns "name" and "owner_name". Therefore + # column owner.name cannot use the label "owner_name", it has + # to be "owner_name_1". + if label in t.c: + _label = label + counter = 1 + while _label in t.c: + _label = label + "_" + str(counter) + counter += 1 + label = _label return coercions.expect(roles.TruncatedLabelRole, label) diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py index 68a6190cf..3fca4e35b 100644 --- a/lib/sqlalchemy/sql/naming.py +++ b/lib/sqlalchemy/sql/naming.py @@ -67,7 +67,7 @@ class ConventionDict(object): return self._column_X(idx).name def _key_column_X_label(self, idx): - return self._column_X(idx)._label + return self._column_X(idx)._ddl_label def _key_referred_table_name(self): fk = self.const.elements[0] diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index af538af0e..4e8f4a397 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -1434,7 +1434,12 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause): "To create indexes with a specific name, create an " "explicit Index object external to the Table." ) - Index(None, self, unique=bool(self.unique), _column_flag=True) + table.append_constraint( + Index( + None, self.key, unique=bool(self.unique), _column_flag=True + ) + ) + elif self.unique: if isinstance(self.unique, util.string_types): raise exc.ArgumentError( |
