diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-04-08 11:59:12 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-04-08 11:59:12 -0400 |
commit | 1b83b588f5573799dee8d508be13c252c2e57115 (patch) | |
tree | ce25d85ed7de9cb3acfa2d38d27c6ea3bc78a393 | |
parent | 3f930732f1363a400e2a8cd0970def28531ae647 (diff) | |
download | sqlalchemy-1b83b588f5573799dee8d508be13c252c2e57115.tar.gz |
- 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.
fixes #3360
-rw-r--r-- | doc/build/changelog/changelog_10.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/default.py | 32 |
2 files changed, 28 insertions, 12 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 149acef9c..281ebfc43 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -20,6 +20,14 @@ .. change:: :tags: bug, mssql + :tickets: 3360 + + 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. + + .. change:: + :tags: bug, mssql :pullreq: github:166 Using the ``Binary`` constructor now present in pymssql rather than diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 3eebc6c06..763e85f82 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -840,18 +840,26 @@ class DefaultExecutionContext(interfaces.ExecutionContext): compiled_params = self.compiled_parameters[0] lastrowid = self.get_lastrowid() - autoinc_col = table._autoincrement_column - if autoinc_col is not None: - # apply type post processors to the lastrowid - proc = autoinc_col.type._cached_result_processor( - self.dialect, None) - if proc is not None: - lastrowid = proc(lastrowid) - self.inserted_primary_key = [ - lastrowid if c is autoinc_col else - compiled_params.get(key_getter(c), None) - for c in table.primary_key - ] + if lastrowid is not None: + autoinc_col = table._autoincrement_column + if autoinc_col is not None: + # apply type post processors to the lastrowid + proc = autoinc_col.type._cached_result_processor( + self.dialect, None) + if proc is not None: + lastrowid = proc(lastrowid) + self.inserted_primary_key = [ + lastrowid if c is autoinc_col else + compiled_params.get(key_getter(c), None) + for c in table.primary_key + ] + else: + # don't have a usable lastrowid, so + # do the same as _setup_ins_pk_from_empty + self.inserted_primary_key = [ + compiled_params.get(key_getter(c), None) + for c in table.primary_key + ] def _setup_ins_pk_from_empty(self): key_getter = self.compiled._key_getters_for_crud_column[2] |