Difference between .filter() and .where() in sqlalchemy

Question:

I’ve seen a few variations of running a query with SQLAlchemy. For example, here is one version:

posts = db.query(models.Post).filter(models.Post.owner_id==user.id).all()

What would be the difference between using the above or using .where? Why are there two variations here?

Asked By: David542

||

Answers:

According to the documentation, there is no difference.

method sqlalchemy.orm.Query.where(*criterion)

A synonym for Query.filter().

It was added in version 1.4 by this commit. According to the commit message the reason to add it was to Convert remaining ORM APIs to support 2.0 style.

You can read more about "2.0 style" here.

Answered By: mkrieger1
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.