]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- ensure that the keys we put into the parameters dictionary
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 8 Apr 2015 16:14:56 +0000 (12:14 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 8 Apr 2015 16:14:56 +0000 (12:14 -0400)
for an insert from select are the string names, and not
the Column objects.  The MSSQL dialect in particular relies upon
checking for these keys in params to know if identity insert
should be on.  references #3360

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/sql/dml.py
test/sql/test_query.py

index 281ebfc436d64bce8a3ba09f17021f44e97a957f..404e6f1c8920ef7cce8007eaf61863a2e1702182 100644 (file)
 
         Fixed a regression where the "last inserted id" mechanics would
         fail to store the correct value for MSSQL on an INSERT where the
-        primary key value was present in the insert params before execution.
+        primary key value was present in the insert params before execution,
+        as well as in the case where an INSERT from SELECT would state the
+        target columns as column objects, instead of string keys.
+
 
     .. change::
         :tags: bug, mssql
index 6a4768fa11a31c0e51dc22959a92e9476950c8ab..a2a5646908237a15162b6db2b63b0ceff03ae1f2 100644 (file)
@@ -10,7 +10,8 @@ Provide :class:`.Insert`, :class:`.Update` and :class:`.Delete`.
 """
 
 from .base import Executable, _generative, _from_objects, DialectKWArgs
-from .elements import ClauseElement, _literal_as_text, Null, and_, _clone
+from .elements import ClauseElement, _literal_as_text, Null, and_, _clone, \
+    _column_as_key
 from .selectable import _interpret_as_from, _interpret_as_select, HasPrefixes
 from .. import util
 from .. import exc
@@ -544,7 +545,8 @@ class Insert(ValuesBase):
                 "This construct already inserts value expressions")
 
         self.parameters, self._has_multi_parameters = \
-            self._process_colparams(dict((n, Null()) for n in names))
+            self._process_colparams(
+                dict((_column_as_key(n), Null()) for n in names))
 
         self.select_names = names
         self.inline = True
index 08afc32569a451112f14f1dcd8ed6dbaf598f9de..98f375018d8ad0163d7a15e6e4792e777565244c 100644 (file)
@@ -275,13 +275,20 @@ class QueryTest(fixtures.TestBase):
         r = t6.insert().values(manual_id=id).execute()
         eq_(r.inserted_primary_key, [12, 1])
 
-    def test_implicit_id_insert_select(self):
+    def test_implicit_id_insert_select_columns(self):
         stmt = users.insert().from_select(
             (users.c.user_id, users.c.user_name),
             users.select().where(users.c.user_id == 20))
 
         testing.db.execute(stmt)
 
+    def test_implicit_id_insert_select_keys(self):
+        stmt = users.insert().from_select(
+            ["user_id", "user_name"],
+            users.select().where(users.c.user_id == 20))
+
+        testing.db.execute(stmt)
+
     def test_row_iteration(self):
         users.insert().execute(
             {'user_id': 7, 'user_name': 'jack'},