Home > other >  Difference between .filter() and .where() in sqlalchemy
Difference between .filter() and .where() in sqlalchemy

Time:07-29

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?

CodePudding user response:

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.

  • Related