MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / filter_by

Method filter_by

lib/sqlalchemy/sql/selectable.py:5652–5709  ·  view source on GitHub ↗

r"""Apply the given filtering criterion as a WHERE clause to this select, using keyword expressions. E.g.:: stmt = select(User).filter_by(name="some name") Multiple criteria may be specified as comma separated; the effect is that they will be joined tog

(self, **kwargs: Any)

Source from the content-addressed store, hash-verified

5650 def scalar_subquery(self) -> ScalarSelect[Any]: ...
5651
5652 def filter_by(self, **kwargs: Any) -> Self:
5653 r"""Apply the given filtering criterion as a WHERE clause
5654 to this select, using keyword expressions.
5655
5656 E.g.::
5657
5658 stmt = select(User).filter_by(name="some name")
5659
5660 Multiple criteria may be specified as comma separated; the effect
5661 is that they will be joined together using the :func:`.and_`
5662 function::
5663
5664 stmt = select(User).filter_by(name="some name", id=5)
5665
5666 The keyword expressions are extracted by searching across **all
5667 entities present in the FROM clause** of the statement. If a
5668 keyword name is present in more than one entity,
5669 :class:`_exc.AmbiguousColumnError` is raised. In this case, use
5670 :meth:`_sql.Select.filter` or :meth:`_sql.Select.where` with
5671 explicit column references::
5672
5673 # both User and Address have an 'id' attribute
5674 stmt = select(User).join(Address).filter_by(id=5)
5675 # raises AmbiguousColumnError
5676
5677 # use filter() with explicit qualification instead
5678 stmt = select(User).join(Address).filter(Address.id == 5)
5679
5680 .. versionchanged:: 2.1
5681
5682 :meth:`_sql.Select.filter_by` now searches across all FROM clause
5683 entities rather than only searching the last joined entity or first
5684 FROM entity. This allows the method to locate attributes
5685 unambiguously across multiple joined tables. The new
5686 :class:`_exc.AmbiguousColumnError` is raised when an attribute name
5687 is present in more than one entity.
5688
5689 See :ref:`change_8601` for migration notes.
5690
5691 .. seealso::
5692
5693 :ref:`tutorial_selecting_data` - in the :ref:`unified_tutorial`
5694
5695 :meth:`_sql.Select.filter` - filter on SQL expressions.
5696
5697 :meth:`_sql.Select.where` - filter on SQL expressions.
5698
5699 """
5700 # Get all entities via plugin system
5701 all_entities = SelectState.get_plugin_class(
5702 self
5703 )._get_filter_by_entities(self)
5704
5705 clauses = [
5706 _entity_namespace_key_search_all(all_entities, key) == value
5707 for key, value in kwargs.items()
5708 ]
5709 return self.filter(*clauses)

Callers

nothing calls this directly

Calls 5

filterMethod · 0.95
get_plugin_classMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected