--- /dev/null
+.. change::
+ :tags: bug, orm
+ :tickets: 4349
+
+ Fixed bug where "dynamic" loader needs to explicitly set the "secondary"
+ table in the FROM clause of the query, to suit the case where the secondary
+ is a join object that is otherwise not pulled into the query from its
+ columns alone.
+
mapper = object_mapper(instance)
prop = mapper._props[self.attr.key]
+
+ if prop.secondary is not None:
+ self._set_select_from([prop.secondary], False)
+
self._criterion = prop._with_parent(
instance,
alias_secondary=False)
query = sess.query(self.attr.target_mapper)
query._criterion = self._criterion
+ query._from_obj = self._from_obj
query._order_by = self._order_by
return query
[Item(id=2)]
)
+ def test_secondary_as_join(self):
+ User, users = self.classes.User, self.tables.users
+ items, orders, order_items, Item = (self.tables.items,
+ self.tables.orders,
+ self.tables.order_items,
+ self.classes.Item)
+
+ mapper(User, users, properties={
+ 'items': relationship(
+ Item,
+ secondary=order_items.join(orders),
+ lazy="dynamic"
+ )
+ })
+ mapper(Item, items)
+
+ sess = create_session()
+ u1 = sess.query(User).first()
+
+ self.assert_compile(
+ u1.items,
+ "SELECT items.id AS items_id, "
+ "items.description AS items_description "
+ "FROM items, order_items JOIN orders "
+ "ON orders.id = order_items.order_id "
+ "WHERE :param_1 = orders.user_id "
+ "AND items.id = order_items.item_id",
+ use_default_dialect=True
+ )
+
def test_transient_count(self):
User, Address = self._user_address_fixture()
u1 = User()