]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The :attr:`.Column.key` attribute is now used as the source of
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 10 Nov 2014 22:37:26 +0000 (17:37 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 10 Nov 2014 22:37:26 +0000 (17:37 -0500)
anonymous bound parameter names within expressions, to match the
existing use of this value as the key when rendered in an INSERT
or UPDATE statement.   This allows :attr:`.Column.key` to be used
as a "substitute" string to work around a difficult column name
that doesn't translate well into a bound parameter name.   Note that
the paramstyle is configurable on :func:`.create_engine` in any case,
and most DBAPIs today support a named and positional style.
fixes #3245

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/sql/elements.py
test/dialect/mssql/test_reflection.py
test/sql/test_compiler.py

index e63e023d907ab1908d2a5477702a92d0f159c302..4e5e1ba1d94b448a368e9864b94dbc9acb55db6c 100644 (file)
     series as well.  For changes that are specific to 1.0 with an emphasis
     on compatibility concerns, see :doc:`/changelog/migration_10`.
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 3245
+
+        The :attr:`.Column.key` attribute is now used as the source of
+        anonymous bound parameter names within expressions, to match the
+        existing use of this value as the key when rendered in an INSERT
+        or UPDATE statement.   This allows :attr:`.Column.key` to be used
+        as a "substitute" string to work around a difficult column name
+        that doesn't translate well into a bound parameter name.   Note that
+        the paramstyle is configurable on :func:`.create_engine` in any case,
+        and most DBAPIs today support a named and positional style.
+
     .. change::
         :tags: bug, sql
         :pullreq: github:146
index fa9b660247ee4c68a915cf4e7aae9b47a922ad20..734f78632b69ef036bda447ae7ff168a6f04020a 100644 (file)
@@ -1092,7 +1092,7 @@ class BindParameter(ColumnElement):
         """
         if isinstance(key, ColumnClause):
             type_ = key.type
-            key = key.name
+            key = key.key
         if required is NO_ARG:
             required = (value is NO_ARG and callable_ is None)
         if value is NO_ARG:
@@ -3335,7 +3335,7 @@ class ColumnClause(Immutable, ColumnElement):
             return name
 
     def _bind_param(self, operator, obj):
-        return BindParameter(self.name, obj,
+        return BindParameter(self.key, obj,
                              _compared_to_operator=operator,
                              _compared_to_type=self.type,
                              unique=True)
index e93162a8e987e788cc419d4bb0f5ad579abb303b..0ef69f656eb26688d1c40c5b4d8dfe7276d60495 100644 (file)
@@ -187,7 +187,7 @@ class InfoCoerceUnicodeTest(fixtures.TestBase, AssertsCompiledSQL):
         stmt = tables.c.table_name == 'somename'
         self.assert_compile(
             stmt,
-            "[TABLES_1].[TABLE_NAME] = :TABLE_NAME_1",
+            "[TABLES_1].[TABLE_NAME] = :table_name_1",
             dialect=dialect
         )
 
@@ -197,7 +197,7 @@ class InfoCoerceUnicodeTest(fixtures.TestBase, AssertsCompiledSQL):
         stmt = tables.c.table_name == 'somename'
         self.assert_compile(
             stmt,
-            "[TABLES_1].[TABLE_NAME] = CAST(:TABLE_NAME_1 AS NVARCHAR(max))",
+            "[TABLES_1].[TABLE_NAME] = CAST(:table_name_1 AS NVARCHAR(max))",
             dialect=dialect
         )
 
index bfafed599fd53cd5149fc25f47b7450d22a689e9..5d1afe6162d0fc0f947e60d2c93355cd3838d0ac 100644 (file)
@@ -435,6 +435,19 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
             dialect=default.DefaultDialect(paramstyle='pyformat')
         )
 
+    def test_anon_param_name_on_keys(self):
+        self.assert_compile(
+            keyed.insert(),
+            "INSERT INTO keyed (x, y, z) VALUES (%(colx)s, %(coly)s, %(z)s)",
+            dialect=default.DefaultDialect(paramstyle='pyformat')
+        )
+        self.assert_compile(
+            keyed.c.coly == 5,
+            "keyed.y = %(coly_1)s",
+            checkparams={'coly_1': 5},
+            dialect=default.DefaultDialect(paramstyle='pyformat')
+        )
+
     def test_dupe_columns(self):
         """test that deduping is performed against clause
         element identity, not rendered result."""