[Sqlalchemy-tickets] [sqlalchemy] #2836: supply distinct within subq eager load when we see that th
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-10-11 20:28:19
|
#2836: supply distinct within subq eager load when we see that the inner query
isn't loading a distinct field
--------------------+---------------------------------------
Reporter: zzzeek | Owner: zzzeek
Type: defect | Status: new
Priority: high | Milestone: 0.9.0
Component: orm | Severity: minor - half an hour
Keywords: | Progress State: in progress
--------------------+---------------------------------------
see https://github.com/zzzeek/sqlalchemy/pull/33 for current work on this.
proposed patch:
{{{
#!diff
diff --git a/lib/sqlalchemy/orm/strategies.py
b/lib/sqlalchemy/orm/strategies.py
index 6ca737c..23e8cb6 100644
--- a/lib/sqlalchemy/orm/strategies.py
+++ b/lib/sqlalchemy/orm/strategies.py
@@ -767,8 +767,17 @@ class SubqueryLoader(AbstractRelationshipLoader):
if not q._from_obj and entity_mapper.isa(leftmost_mapper):
q._set_select_from([entity_mapper], False)
+ target_cols = q._adapt_col_list(leftmost_attr)
+
# select from the identity columns of the outer
- q._set_entities(q._adapt_col_list(leftmost_attr))
+ q._set_entities(target_cols)
+
+ # if target_cols refer to a non-primary key or only
+ # part of a composite primary key, set the q as distinct
+ for c in target_cols:
+ if not set(target_cols).issuperset(c.table.primary_key):
+ q._distinct = True
+ break
if q._order_by is False:
q._order_by = leftmost_mapper.order_by
}}}
we'll try to backport an "experimental" version of this to 0.8/0.7.
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2836>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|