]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Don't select lastrowid for inline=True
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Dec 2016 15:16:31 +0000 (10:16 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Dec 2016 18:41:25 +0000 (13:41 -0500)
- Fixed bug where SQL Server dialects would attempt to select the
last row identity for an INSERT from SELECT, failing in the case when
the SELECT has no rows.  For such a statement,
the inline flag is set to True indicating no last primary key
should be fetched.

Change-Id: Ic40d56d9eadadc3024a4d71245f9eed4c420024a
Fixes: #3876
doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/testing/suite/test_insert.py

index 6b842ab077a11fd7793020bfe20da3ed8188dc68..8ac24188932940c47f0a760178ea035edc566942 100644 (file)
 
         Fixed bug where an INSERT from SELECT where the source table contains
         an autoincrementing Sequence would fail to compile correctly.
+    .. change:: 3876
+        :tags: bug, mssql
+        :tickets: 3876
+
+        Fixed bug where SQL Server dialects would attempt to select the
+        last row identity for an INSERT from SELECT, failing in the case when
+        the SELECT has no rows.  For such a statement,
+        the inline flag is set to True indicating no last primary key
+        should be fetched.
 
     .. change:: 3875
         :tags: bug, oracle
index 19558a2d6aa82f42858dbfef7e49474df61c33dd..1a1f7f06de98d06fca7e51ece5e1564f9fb37e67 100644 (file)
@@ -1022,7 +1022,8 @@ class MSExecutionContext(default.DefaultExecutionContext):
             else:
                 self._enable_identity_insert = False
 
-            self._select_lastrowid = insert_has_sequence and \
+            self._select_lastrowid = not self.compiled.inline and \
+                insert_has_sequence and \
                 not self.compiled.returning and \
                 not self._enable_identity_insert and \
                 not self.executemany
index 70e8a6b17b2964b6ca141764ee5d4885ff4ef517..c0b6b18ebd9458a4f3264917f3450e0c8f97d22f 100644 (file)
@@ -140,6 +140,56 @@ class InsertBehaviorTest(fixtures.TablesTest):
 
         assert len(r.fetchall())
 
+    @requirements.insert_from_select
+    def test_insert_from_select_autoinc(self):
+        src_table = self.tables.manual_pk
+        dest_table = self.tables.autoinc_pk
+        config.db.execute(
+            src_table.insert(),
+            [
+                dict(id=1, data="data1"),
+                dict(id=2, data="data2"),
+                dict(id=3, data="data3"),
+            ]
+        )
+
+        result = config.db.execute(
+            dest_table.insert().
+            from_select(
+                ("data",),
+                select([src_table.c.data]).
+                where(src_table.c.data.in_(["data2", "data3"]))
+            )
+        )
+
+        eq_(result.inserted_primary_key, [None])
+
+        result = config.db.execute(
+            select([dest_table.c.data]).order_by(dest_table.c.data)
+        )
+        eq_(result.fetchall(), [("data2", ), ("data3", )])
+
+    @requirements.insert_from_select
+    def test_insert_from_select_autoinc_no_rows(self):
+        src_table = self.tables.manual_pk
+        dest_table = self.tables.autoinc_pk
+
+        result = config.db.execute(
+            dest_table.insert().
+            from_select(
+                ("data",),
+                select([src_table.c.data]).
+                where(src_table.c.data.in_(["data2", "data3"]))
+            )
+        )
+        eq_(result.inserted_primary_key, [None])
+
+        result = config.db.execute(
+            select([dest_table.c.data]).order_by(dest_table.c.data)
+        )
+
+        eq_(result.fetchall(), [])
+
     @requirements.insert_from_select
     def test_insert_from_select(self):
         table = self.tables.manual_pk