diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-02-11 17:00:47 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-02-12 16:55:48 -0500 |
commit | c1f310df44033d943413170de878ce95fafa387e (patch) | |
tree | 1c053d5a0bf8610393ba38bbb19a576383da357b /lib/sqlalchemy/sql/crud.py | |
parent | bb7b353d6f97184d2689c8c682bab5caac4ec1e7 (diff) | |
download | sqlalchemy-c1f310df44033d943413170de878ce95fafa387e.tar.gz |
Allow SQL expression for ORM primary keys
A SQL expression can now be assigned to a primary key attribute for an ORM
flush in the same manner as ordinary attributes as described in
:ref:`flush_embedded_sql_expressions` where the expression will be evaulated
and then returned to the ORM using RETURNING, or in the case of pysqlite,
works using the cursor.lastrowid attribute.Requires either a database that
supports RETURNING (e.g. Postgresql, Oracle, SQL Server) or pysqlite.
Fixes: #3133
Fixes: #4494
Change-Id: I83da8357354de002cb04fa4a553f2a2f90c5157d
Diffstat (limited to 'lib/sqlalchemy/sql/crud.py')
-rw-r--r-- | lib/sqlalchemy/sql/crud.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/crud.py b/lib/sqlalchemy/sql/crud.py index cc72073ca..6c9b8ee5b 100644 --- a/lib/sqlalchemy/sql/crud.py +++ b/lib/sqlalchemy/sql/crud.py @@ -409,7 +409,12 @@ def _append_param_parameter( compiler.returning.append(c) value = compiler.process(value.self_group(), **kw) else: - compiler.postfetch.append(c) + # postfetch specifically means, "we can SELECT the row we just + # inserted by primary key to get back the server generated + # defaults". so by definition this can't be used to get the primary + # key value back, because we need to have it ahead of time. + if not c.primary_key: + compiler.postfetch.append(c) value = compiler.process(value.self_group(), **kw) values.append((c, value)) |