summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2023-02-05 16:40:43 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2023-02-05 16:40:43 +0000
commit2459619e751f39a796bb1dd9fe75947dd0961fee (patch)
tree37b4197c034b803f91b2646df2300bab4b15a7e4 /lib/sqlalchemy
parente8f5a9277e2ffc674cde99114845b866d7e10b51 (diff)
parent92e3c21ea9b8192ff3d6ad856389186dfe8b3d3d (diff)
downloadsqlalchemy-2459619e751f39a796bb1dd9fe75947dd0961fee.tar.gz
Merge "dont add non-server-side cols to returning for versioning" into main
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/mapper.py20
-rw-r--r--lib/sqlalchemy/orm/persistence.py13
2 files changed, 27 insertions, 6 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index a3b209e4a..bb7e470ff 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -83,6 +83,7 @@ from ..sql import util as sql_util
from ..sql import visitors
from ..sql.cache_key import MemoizedHasCacheKey
from ..sql.elements import KeyedColumnElement
+from ..sql.schema import Column
from ..sql.schema import Table
from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
from ..util import HasMemoized
@@ -112,7 +113,6 @@ if TYPE_CHECKING:
from ..sql.base import ReadOnlyColumnCollection
from ..sql.elements import ColumnClause
from ..sql.elements import ColumnElement
- from ..sql.schema import Column
from ..sql.selectable import FromClause
from ..util import OrderedSet
@@ -2523,6 +2523,24 @@ class Mapper(
return from_obj
@HasMemoized.memoized_attribute
+ def _version_id_has_server_side_value(self) -> bool:
+ vid_col = self.version_id_col
+
+ if vid_col is None:
+ return False
+
+ elif not isinstance(vid_col, Column):
+ return True
+ else:
+ return vid_col.server_default is not None or (
+ vid_col.default is not None
+ and (
+ not vid_col.default.is_scalar
+ and not vid_col.default.is_callable
+ )
+ )
+
+ @HasMemoized.memoized_attribute
def _single_table_criterion(self):
if self.single and self.inherits and self.polymorphic_on is not None:
return self.polymorphic_on._annotate(
diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py
index cc7e321b4..b8368001b 100644
--- a/lib/sqlalchemy/orm/persistence.py
+++ b/lib/sqlalchemy/orm/persistence.py
@@ -801,7 +801,7 @@ def _emit_update_statements(
)
return_defaults = True
- if mapper.version_id_col is not None:
+ if mapper._version_id_has_server_side_value:
statement = statement.return_defaults(mapper.version_id_col)
return_defaults = True
@@ -1268,13 +1268,16 @@ def _emit_post_update_statements(
stmt = table.update().where(clauses)
- if mapper.version_id_col is not None:
- stmt = stmt.return_defaults(mapper.version_id_col)
-
return stmt
statement = base_mapper._memo(("post_update", table), update_stmt)
+ if mapper._version_id_has_server_side_value:
+ statement = statement.return_defaults(mapper.version_id_col)
+ return_defaults = True
+ else:
+ return_defaults = False
+
# execute each UPDATE in the order according to the original
# list of states to guarantee row access order, but
# also group them into common (connection, cols) sets
@@ -1290,7 +1293,7 @@ def _emit_post_update_statements(
assert_singlerow = (
connection.dialect.supports_sane_rowcount
- if mapper.version_id_col is None
+ if not return_defaults
else connection.dialect.supports_sane_rowcount_returning
)
assert_multirow = (