]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed a regression where the "last inserted id" mechanics would
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 8 Apr 2015 15:59:12 +0000 (11:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 8 Apr 2015 15:59:12 +0000 (11:59 -0400)
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

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/engine/default.py

index 149acef9c7a157074d11524b955ca36d208ae19c..281ebfc436d64bce8a3ba09f17021f44e97a957f 100644 (file)
 .. changelog::
     :version: 1.0.0
 
+    .. 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
index 3eebc6c06384f527d0a2fb7e24c0cd63b6cb1ca4..763e85f82123e5a7201c5d5a84e5d1f33b96196e 100644 (file)
@@ -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]