by writing our SQL to load a subset of elements for collections or
scalar attributes.
+.. tip:: SQLAlchemy now has a **much simpler way to do this**, by allowing
+ WHERE criteria to be added directly to loader options such as
+ :func:`_orm.joinedload`
+ and :func:`_orm.selectinload` using :meth:`.PropComparator.and_`. See
+ the section :ref:`loader_option_criteria` for examples.
+
+ The techniques described here still apply if the related collection is
+ to be queried using SQL criteria or modifiers more complex than a simple
+ WHERE clause.
+
+
As an example, we can load a ``User`` object and eagerly load only particular
addresses into its ``.addresses`` collection by filtering the joined data,
routing it using :func:`_orm.contains_eager`, also using
assuming default session settings, or the :meth:`.Session.expire_all`
or :meth:`.Session.expire` methods are used.
+.. seealso::
+
+ :ref:`loader_option_criteria` - modern API allowing WHERE criteria directly
+ within any relationship loader option
+
Relationship Loader API
-----------------------
if isinstance(name_or_url, str):
return _parse_url(name_or_url)
+ elif not isinstance(name_or_url, URL) and not hasattr(
+ name_or_url, "_sqla_is_testing_if_this_is_a_mock_object"
+ ):
+ raise exc.ArgumentError(
+ f"Expected string or URL object, got {name_or_url!r}"
+ )
else:
return name_or_url