πŸ’­ Filter Data - WHERE - SQLModel

!https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel

Copy this post

When fetching pydantic models from the database with sqlmodel, and you cannot select your item by id, you probably need to use a where clause. This is the sqlmodel way of doing it.

Here is a snippet of how I am using sqlmodel select and where to find a post by link in my thoughts database.

@post_router.get("/link/")
async def get_post_by_link(
    *,
    session: Session = Depends(get_session),
    link: str,
) -> PostRead:
    "get one post by link"
    link = urllib.parse.unquote(link)
    print(f'link: {link}')
    post = session.exec(select(Post).where(Post.link==link)).first()
    if not post:
        raise HTTPException(status_code=404, detail=f"Post not found for link: {link}")

    return post

Note

This post is a thought. It’s a short note that I make about someone else’s content online #thoughts