summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-05-08 22:49:33 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-05-09 09:49:50 -0400
commitd972b0f4ed4cd55c3f8e422816b32e9081168513 (patch)
treebed6eabf2708bf19a8d1024dd73b95672431653c /lib/sqlalchemy/sql/compiler.py
parent47c91d06b56b0a0cf366d3c1f8b6d71a82149e43 (diff)
downloadsqlalchemy-d972b0f4ed4cd55c3f8e422816b32e9081168513.tar.gz
explicitly fetch inserted pk for values(pkcol=None)
Altered the compilation mechanics of the :class:`.Insert` construct such that the "autoincrement primary key" column value will be fetched via ``cursor.lastrowid`` or RETURNING even if present in the parameter set or within the :meth:`.Insert.values` method as a plain bound value, for single-row INSERT statements on specific backends that are known to generate autoincrementing values even when explicit NULL is passed. This restores a behavior that was in the 1.3 series for both the use case of separate parameter set as well as :meth:`.Insert.values`. In 1.4, the parameter set behavior unintentionally changed to no longer do this, but the :meth:`.Insert.values` method would still fetch autoincrement values up until 1.4.21 where :ticket:`6770` changed the behavior yet again again unintentionally as this use case was never covered. The behavior is now defined as "working" to suit the case where databases such as SQLite, MySQL and MariaDB will ignore an explicit NULL primary key value and nonetheless invoke an autoincrement generator. Fixes: #7998 Change-Id: I5d4105a14217945f87fbe9a6f2a3c87f6ef20529
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py38
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 19435b59c..f774028f2 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1560,14 +1560,36 @@ class SQLCompiler(Compiled):
for col in table.primary_key
]
+ autoinc_getter = 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(
+ lastrowid_processor = autoinc_col.type._cached_result_processor(
self.dialect, None
)
+ autoinc_key = param_key_getter(autoinc_col)
+
+ # if a bind value is present for the autoincrement column
+ # in the parameters, we need to do the logic dictated by
+ # #7998; honor a non-None user-passed parameter over lastrowid.
+ # previously in the 1.4 series we weren't fetching lastrowid
+ # at all if the key were present in the parameters
+ if autoinc_key in self.binds:
+
+ def autoinc_getter(lastrowid, parameters):
+ param_value = parameters.get(autoinc_key, lastrowid)
+ if param_value is not None:
+ # they supplied non-None parameter, use that.
+ # SQLite at least is observed to return the wrong
+ # cursor.lastrowid for INSERT..ON CONFLICT so it
+ # can't be used in all cases
+ return param_value
+ else:
+ # use lastrowid
+ return lastrowid
+
else:
- proc = None
+ lastrowid_processor = None
row_fn = result.result_tuple([col.key for col in table.primary_key])
@@ -1578,14 +1600,20 @@ class SQLCompiler(Compiled):
that were sent along with the INSERT.
"""
- if proc is not None:
- lastrowid = proc(lastrowid)
+ if lastrowid_processor is not None:
+ lastrowid = lastrowid_processor(lastrowid)
if lastrowid is None:
return row_fn(getter(parameters) for getter, col in getters)
else:
return row_fn(
- lastrowid if col is autoinc_col else getter(parameters)
+ (
+ autoinc_getter(lastrowid, parameters)
+ if autoinc_getter
+ else lastrowid
+ )
+ if col is autoinc_col
+ else getter(parameters)
for getter, col in getters
)