]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add template methods for ORMInsert
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 31 Mar 2022 18:56:52 +0000 (14:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 31 Mar 2022 18:58:26 +0000 (14:58 -0400)
Fixed regression caused by :ticket:`7861` where invoking an
:class:`.Insert` construct which contained ORM entities via
:meth:`_orm.Session.execute` would fail.

Fixes: #7878
Change-Id: Icc4d8028249cc417f504fdd3e31e206b5bbc89f8
(cherry picked from commit cbe38dbc667436f5da74ce7c3d6e5451f41c62e2)

doc/build/changelog/unreleased_14/7878.rst [new file with mode: 0644]
lib/sqlalchemy/orm/persistence.py
test/orm/test_session.py

diff --git a/doc/build/changelog/unreleased_14/7878.rst b/doc/build/changelog/unreleased_14/7878.rst
new file mode 100644 (file)
index 0000000..6c9e929
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, orm, regression
+    :tickets: 7878
+
+    Fixed regression caused by :ticket:`7861` where invoking an
+    :class:`.Insert` construct which contained ORM entities directly via
+    :meth:`_orm.Session.execute` would fail.
index c3d2cfd49c92357dc6e31e181ede3c785f2e357b..654e659f411b93c796e228bf98c808325f5adfad 100644 (file)
@@ -2186,7 +2186,32 @@ class ORMDMLState:
 
 @CompileState.plugin_for("orm", "insert")
 class ORMInsert(ORMDMLState, InsertDMLState):
-    pass
+    @classmethod
+    def orm_pre_session_exec(
+        cls,
+        session,
+        statement,
+        params,
+        execution_options,
+        bind_arguments,
+        is_reentrant_invoke,
+    ):
+        return (
+            statement,
+            util.immutabledict(execution_options),
+        )
+
+    @classmethod
+    def orm_setup_cursor_result(
+        cls,
+        session,
+        statement,
+        params,
+        execution_options,
+        bind_arguments,
+        result,
+    ):
+        return result
 
 
 @CompileState.plugin_for("orm", "update")
index 295fd8205f3a9e3cc00f194b6449c626b5004229..83ce629700c1ad1ea6f1c53b89c31fea8afc54e9 100644 (file)
@@ -1,8 +1,10 @@
 import inspect as _py_inspect
 
 import sqlalchemy as sa
+from sqlalchemy import delete
 from sqlalchemy import event
 from sqlalchemy import ForeignKey
+from sqlalchemy import insert
 from sqlalchemy import inspect
 from sqlalchemy import Integer
 from sqlalchemy import select
@@ -10,6 +12,7 @@ from sqlalchemy import Sequence
 from sqlalchemy import String
 from sqlalchemy import testing
 from sqlalchemy import text
+from sqlalchemy import update
 from sqlalchemy.orm import attributes
 from sqlalchemy.orm import backref
 from sqlalchemy.orm import close_all_sessions
@@ -2181,6 +2184,32 @@ class NewStyleExecutionTest(_fixtures.FixtureTest):
         ):
             result.all()
 
+    @testing.combinations("insert", "update", "delete", argnames="dml_expr")
+    @testing.combinations("core", "orm", argnames="coreorm")
+    def test_dml_execute(self, dml_expr, coreorm):
+        User = self.classes.User
+        users = self.tables.users
+
+        sess = fixture_session()
+
+        if coreorm == "orm":
+            if dml_expr == "insert":
+                stmt = insert(User).values(id=12, name="some user")
+            elif dml_expr == "update":
+                stmt = update(User).values(name="sone name").filter_by(id=15)
+            else:
+                stmt = delete(User).filter_by(id=15)
+        else:
+            if dml_expr == "insert":
+                stmt = insert(users).values(id=12, name="some user")
+            elif dml_expr == "update":
+                stmt = update(users).values(name="sone name").filter_by(id=15)
+            else:
+                stmt = delete(users).filter_by(id=15)
+
+        result = sess.execute(stmt)
+        result.close()
+
     @testing.combinations((True,), (False,), argnames="prebuffered")
     @testing.combinations(("close",), ("expunge_all",), argnames="meth")
     def test_unbuffered_result_before_session_is_closed(