---
title: "Proper handling of None in WHERE condition · Issue #109 · fast..."
description: "!https://github.com/fastapi/sqlmodel/issues/109#issuecomment-1046223225"
date: 2024-11-08
published: true
tags:
  - database
  - python
  - sqlmodel
  - thought
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://github.com/fastapi/sqlmodel/issues/109#issuecomment-1046223225" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-image">
      <img src="https://opengraph.githubassets.com/4ff8c5e0c53534cd26b8f6dfcad14ae1a8ae0fceb183a0517a84de53aa07b8d5/fastapi/sqlmodel/issues/109" alt="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&#39;t find it. I searched the SQLModel documentation, with the integrated search. I..." loading="lazy">
    </div>
    <div class="embed-card-content">
      <div class="embed-card-title">Proper handling of None in WHERE condition · Issue #109 · fastapi/sqlmodel</div>
      <div class="embed-card-description">First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn&#39;t find it. I searched the SQLModel documentation, with the integrated search. I...</div>
      <div class="embed-card-meta">GitHub &middot; github.com</div>
    </div>
  </a>
</div>


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 itentity not equality.
