diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-10-13 20:33:24 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-10-14 12:51:14 -0400 |
commit | 41dc71ad2fc1963a44e5f308f53aed6b8d7d662a (patch) | |
tree | fb678b21f16469e591326511530bb1cec6e57772 /lib/sqlalchemy/sql/elements.py | |
parent | 8a55fb6017b01c5b7503be2bedfa82b9663f8a94 (diff) | |
download | sqlalchemy-41dc71ad2fc1963a44e5f308f53aed6b8d7d662a.tar.gz |
Use separate label generator for column_label naming convention
Fixed bug where a table that would have a column label overlap with a plain
column name, such as "foo.id AS foo_id" vs. "foo.foo_id", would prematurely
generate the ``._label`` attribute for a column before this overlap could
be detected due to the use of the ``index=True`` or ``unique=True`` flag on
the column in conjunction with the default naming convention of
``"column_0_label"``. This would then lead to failures when ``._label``
were used later to generate a bound parameter name, in particular those
used by the ORM when generating the WHERE clause for an UPDATE statement.
The issue has been fixed by using an alternate ``._label`` accessor for DDL
generation that does not affect the state of the :class:`.Column`. The
accessor also bypasses the key-deduplication step as it is not necessary
for DDL, the naming is now consistently ``"<tablename>_<columnname>"``
without any subsequent numeric symbols when used in DDL.
Fixes: #4911
Change-Id: Iabf5fd3250738d800d6e41a2a3a27a7ce2405e7d
Diffstat (limited to 'lib/sqlalchemy/sql/elements.py')
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 37 |
1 files changed, 21 insertions, 16 deletions
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) |