Python SQLAlchemy PostgreSQL find by primary key Deprecate message

Question:

I use below code to find the object by primary key. Now I am getting this Deprecated features detected message.

How can I re-write this query to fix the deprecated message.

Code:

  def find_by_id(self, obj_id):
    with self.session() as s:
        x = s.query(User).get(obj_id)
        return x

Warning:

  LegacyAPIWarning: Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. To prevent incompati
ble upgrades prior to updating applications, ensure requirements files are pinned to "sqlalchemy<2.0". Set environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings.  Set environment variable SQLALCHEMY_SILENCE_UBER
_WARNING=1 to silence this message. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
    x = s.query(User).get(obj_id)
Asked By: sfgroups

||

Answers:

Try filter_by instead of get.

def find_by_id(self, obj_id):
    with self.session() as s:
        x = s.query(User).filter_by(id=obj_id).first()
        return x
Answered By: iohans