diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-08 22:49:33 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-09 09:49:50 -0400 |
commit | d972b0f4ed4cd55c3f8e422816b32e9081168513 (patch) | |
tree | bed6eabf2708bf19a8d1024dd73b95672431653c /lib/sqlalchemy/sql/crud.py | |
parent | 47c91d06b56b0a0cf366d3c1f8b6d71a82149e43 (diff) | |
download | sqlalchemy-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/crud.py')
-rw-r--r-- | lib/sqlalchemy/sql/crud.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/crud.py b/lib/sqlalchemy/sql/crud.py index d3329c391..6a5785043 100644 --- a/lib/sqlalchemy/sql/crud.py +++ b/lib/sqlalchemy/sql/crud.py @@ -528,6 +528,17 @@ def _scan_cols( else: cols = stmt.table.columns + if compile_state.isinsert and not compile_state._has_multi_parameters: + # new rules for #7998. fetch lastrowid or implicit returning + # for autoincrement column even if parameter is NULL, for DBs that + # override NULL param for primary key (sqlite, mysql/mariadb) + autoincrement_col = stmt.table._autoincrement_column + insert_null_pk_still_autoincrements = ( + compiler.dialect.insert_null_pk_still_autoincrements + ) + else: + autoincrement_col = insert_null_pk_still_autoincrements = None + for c in cols: # scan through every column in the target table @@ -547,6 +558,8 @@ def _scan_cols( implicit_returning, implicit_return_defaults, values, + autoincrement_col, + insert_null_pk_still_autoincrements, kw, ) @@ -626,6 +639,8 @@ def _append_param_parameter( implicit_returning, implicit_return_defaults, values, + autoincrement_col, + insert_null_pk_still_autoincrements, kw, ): value = parameters.pop(col_key) @@ -635,6 +650,19 @@ def _append_param_parameter( ) if coercions._is_literal(value): + + if ( + insert_null_pk_still_autoincrements + and c.primary_key + and c is autoincrement_col + ): + # support use case for #7998, fetch autoincrement cols + # even if value was given + if implicit_returning: + compiler.implicit_returning.append(c) + elif compiler.dialect.postfetch_lastrowid: + compiler.postfetch_lastrowid = True + value = _create_bind_param( compiler, c, @@ -646,6 +674,19 @@ def _append_param_parameter( **kw, ) elif value._is_bind_parameter: + if ( + insert_null_pk_still_autoincrements + and value.value is None + and c.primary_key + and c is autoincrement_col + ): + # support use case for #7998, fetch autoincrement cols + # even if value was given + if implicit_returning: + compiler.implicit_returning.append(c) + elif compiler.dialect.postfetch_lastrowid: + compiler.postfetch_lastrowid = True + value = _handle_values_anonymous_param( compiler, c, @@ -1244,7 +1285,6 @@ def _get_returning_modifiers(compiler, stmt, compile_state, toplevel): and not stmt._returning and not compile_state._has_multi_parameters ) - implicit_returning = ( need_pks and compiler.dialect.implicit_returning @@ -1271,7 +1311,6 @@ def _get_returning_modifiers(compiler, stmt, compile_state, toplevel): implicit_return_defaults = set(stmt._return_defaults_columns) postfetch_lastrowid = need_pks and compiler.dialect.postfetch_lastrowid - return ( need_pks, implicit_returning, |