IN Clause in SQLAlchemy ORM Query Object

tech kamar
Jul 10, 2024

To retrieve a items based on list of ids use

class Post(Base):
__tablename__ = "post"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("user.id"))
content = Column(String(255),nullable=False)
created_date = Column(TIMESTAMP(timezone=False), nullable=False, default=datetime.now())
updated_date = Column(TIMESTAMP(timezone=False), nullable=False, default=datetime.now())

# Now we need to retrieve posts by ids 1,2 & 3
# Query to retrieve based on ID
posts = db_session.query(Post).filter(Post.id.in_((1,2,3))).all()

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response

Recommended from Medium

Lists