---
title: "💭 Filter Data - WHERE - SQLModel"
description: "!https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel"
date: 2023-07-28
published: true
tags:
  - python
  - fastapi
  - sqlmodel
  - thought
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-image">
      <img src="https://sqlmodel.tiangolo.com/assets/images/social/tutorial/where.png" alt="Filter Data - WHERE - SQLModel — SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness." loading="lazy">
    </div>
    <div class="embed-card-content">
      <div class="embed-card-title">Filter Data - WHERE - SQLModel</div>
      <div class="embed-card-description">SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.</div>
      <div class="embed-card-meta">sqlmodel.tiangolo.com</div>
    </div>
  </a>
</div>


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.

``` python
@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 <a href="/thoughts/" class="wikilink" data-title="Thoughts" data-description="These are generally my thoughts on a web page or some sort of url, except a rare few don&#39;t have a link. These are dual published off of my..." data-date="2024-04-01">thought</a>. It's a short note that I make
    about someone else's content online <a href="/tags/thoughts/" class="hashtag-tag" data-tag="thoughts" data-count=2 data-reading-time=3 data-reading-time-text="3 minutes">#thoughts</a>
