Proper handling of None in WHERE condition · Issue #109 · fastapi/sqlmodel
First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I...
GitHub · github.com [1]
SQLModel models ship with an is_, and is_not that you can use to compare to None without pesky linters complaining.
This comment summed it up quite well.
I believe this is concerned entirely with SQLAlchemy, not with SQLModel, and has to do with the required semantics to construct a BinaryExpression object.
Hero.age == None evaluates to a BinaryExpression object which is eventually used to construct the SQL query that the SQLAlchemy engine issues to your DBMS.
Hero.age is None evaluates to False immediately, and not a BinaryExpression, which short-circuits the query no matter the value of age in a row.
From a cursory search, it does not seem that the is operator can be overridden in Python. This could help explain why the only possibility is by using the == operator, which can be overridden.
so rather than using Team.heros == None we can use Team.seros.is_(None) which checks for...
Posts tagged: sqlmodel
All posts with the tag "sqlmodel"
3 posts
latest post 2024-11-08
Publishing rhythm
Read a Range of Data - LIMIT and OFFSET - SQLModel
SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
sqlmodel.tiangolo.com [1]
Today I was running some sqlmodel queries through the sqlalchemy orm. Admittedly I’ve not done enough orm queries before, and I’ve done quite a bit of raw sql. I was trying to get objects from two separate models that had relationships setup.
session.query(User, Images).where(User.id == 3).all()
It is incredibly slow, and gives me the following warning.
SELECT statement has a cartesian product between FROM element(s)
What I learned from the SQLModel docs is that you should give it a join to correct this and go much faster.
session.query(User, Images).join(Images).where(User.id == 3).all()
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://sqlmodel.tiangolo.com/tutorial/limit-and-offset/
[2]: /thoughts/
Filter Data - WHERE - SQLModel
SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
sqlmodel.tiangolo.com [1]
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 [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel
[2]: /thoughts/