From: Mike Bayer Date: Wed, 8 Apr 2015 16:14:56 +0000 (-0400) Subject: - ensure that the keys we put into the parameters dictionary X-Git-Tag: rel_1_0_0~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07153dc0926568b35a907241d8e954ecf0ca54f1;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - ensure that the keys we put into the parameters dictionary 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 --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 281ebfc436..404e6f1c89 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -24,7 +24,10 @@ 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 diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 6a4768fa11..a2a5646908 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -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 diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 08afc32569..98f375018d 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -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'},