loader options from non_primary mappers

Issue #3348 new
Mike Bayer repo owner created an issue

e.g. query.options(joinedload_all("a.bs_np.c")), where "bs_np" refers to a non primary mapper. can't use getattr() here:

diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py
index cb7a5fe..70f4caa 100644
--- a/lib/sqlalchemy/orm/strategy_options.py
+++ b/lib/sqlalchemy/orm/strategy_options.py
@@ -128,21 +128,24 @@ class Load(Generative, MapperOption):
                     attr = "%s:%s" % (wildcard_key, attr)
                 return path.token(attr)

-            try:
-                # use getattr on the class to work around
-                # synonyms, hybrids, etc.
-                attr = getattr(path.entity.class_, attr)
-            except AttributeError:
-                if raiseerr:
-                    raise sa_exc.ArgumentError(
-                        "Can't find property named '%s' on the "
-                        "mapped entity %s in this Query. " % (
-                            attr, path.entity)
-                    )
-                else:
-                    return None
+            if path.entity.non_primary:
+                attr = path.entity._props[attr]
             else:
-                attr = attr.property
+                try:
+                    # use getattr on the class to work around
+                    # synonyms, hybrids, etc.
+                    attr = getattr(path.entity.class_, attr)
+                except AttributeError:
+                    if raiseerr:
+                        raise sa_exc.ArgumentError(
+                            "Can't find property named '%s' on the "
+                            "mapped entity %s in this Query. " % (
+                                attr, path.entity)
+                        )
+                    else:
+                        return None
+                else:
+                    attr = attr.property

             path = path[attr]
         else:

this might apply to 0.9 as well.

Comments (5)

  1. Mike Bayer reporter
    • changed milestone to 1.1

    this is probably something that's never worked so lets make sure we get to it...

  2. Log in to comment