summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/mapper.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-03-22 20:14:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-03-23 12:12:34 -0400
commitd051645463b169bf1535459653eff247cb772e62 (patch)
tree67fff8dc12314e1d96257a265daf5e7dce6f7e67 /lib/sqlalchemy/orm/mapper.py
parent6652c62bd90a455843c77f41acd50af920126351 (diff)
downloadsqlalchemy-d051645463b169bf1535459653eff247cb772e62.tar.gz
trust user PK argument as given; don't reduce
Fixed issue where the :class:`_orm.Mapper` would reduce a user-defined :paramref:`_orm.Mapper.primary_key` argument too aggressively, in the case of mapping to a ``UNION`` where for some of the SELECT entries, two columns are essentially equivalent, but in another, they are not, such as in a recursive CTE. The logic here has been changed to accept a given user-defined PK as given, where columns will be related to the mapped selectable but no longer "reduced" as this heuristic can't accommodate for all situations. Fixes: #7842 Change-Id: Ie46f0a3d42cae0501641fa213da0a9d5ca26c3ad
Diffstat (limited to 'lib/sqlalchemy/orm/mapper.py')
-rw-r--r--lib/sqlalchemy/orm/mapper.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 011e7d2ef..7d1fc7643 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -1368,17 +1368,17 @@ class Mapper(
# that of the inheriting (unless concrete or explicit)
self.primary_key = self.inherits.primary_key
else:
- # determine primary key from argument or persist_selectable pks -
- # reduce to the minimal set of columns
+ # determine primary key from argument or persist_selectable pks
if self._primary_key_argument:
- primary_key = sql_util.reduce_columns(
- [
- self.persist_selectable.corresponding_column(c)
- for c in self._primary_key_argument
- ],
- ignore_nonexistent_tables=True,
- )
+ primary_key = [
+ self.persist_selectable.corresponding_column(c)
+ for c in self._primary_key_argument
+ ]
else:
+ # if heuristically determined PKs, reduce to the minimal set
+ # of columns by eliminating FK->PK pairs for a multi-table
+ # expression. May over-reduce for some kinds of UNIONs
+ # / CTEs; use explicit PK argument for these special cases
primary_key = sql_util.reduce_columns(
self._pks_by_table[self.persist_selectable],
ignore_nonexistent_tables=True,