Skip to content

supply distinct within subq eager load when we see that the inner query isn't loading a distinct field #2836

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

see zzzeek/sqlalchemy#33 for current work on this.

proposed patch:

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](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.

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Oct 13, 2013

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

f00544a master

28a3491 0.8

sqlalchemy-bot

sqlalchemy-bot commented on Oct 13, 2013

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
added this to the 0.9.0 milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          supply distinct within subq eager load when we see that the inner query isn't loading a distinct field · Issue #2836 · sqlalchemy/sqlalchemy