From c1f310df44033d943413170de878ce95fafa387e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 11 Feb 2019 17:00:47 -0500 Subject: 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 --- lib/sqlalchemy/sql/crud.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql') 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)) -- cgit v1.2.1