Sling Academy
Home/SQLAlchemy/SQLAlchemy: Select rows where column is Null or empty

SQLAlchemy: Select rows where column is Null or empty

Last updated: January 04, 2024

Introduction

SQLAlchemy provides a robust way to interact with databases in Python. Filtering rows by checking if a column is NULL or empty is a common requirement. In this tutorial, we’ll master how to execute this using SQLAlchemy elegantly.

Setting Up Our Environment

Before proceeding, ensure you have the following prerequisites installed:

  • Python (3.x recommended)
  • SQLAlchemy
  • A database engine (e.g., SQLite, PostgreSQL)

To install SQLAlchemy, run:

pip install SQLAlchemy

For this tutorial, we’ll use the SQLite database for simplicity.

Defining Our Model

First, we need to define our model representing the table we’ll query against.

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String)

engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

Adding Dummy Data

Let’s add some example data into our User table for our queries.

users = [
    User(name='Alice', email='[email protected]'),
    User(name='Bob', email=None),  
    User(name='', email='[email protected]'),
    User(name=None, email='[email protected]')
]

session.bulk_save_objects(users)
session.commit()

Basic Query for NULL or Empty Fields

To find rows where a certain column is NULL or empty, we use or_ and == constructs.

from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import or_

with Session(engine) as session:
    null_or_empty_emails = session.query(User).filter(or_(User.email == None, User.email == '')).all()

for user in null_or_empty_emails:
    print(f'{user.name} {user.email}')

This will find any users where the email attribute is either NULL or an empty string.

Advanced Use Case: Handling Whitespace and Custom Conditions

In more advanced scenarios, we may want to consider strings that contain only whitespace as empty. SQLAlchemy’s func offers the ability to invoke database functions such as TRIM.

from sqlalchemy import func

with Session(engine) as session:
    null_empty_or_whitespace_emails = session.query(User).filter(or_(User.email == None, func.trim(User.email) == '')).all()

for user in null_empty_or_whitespace_emails:
    print(f'{user.name} {user.email}')

Using func.trim, we treat any user with an email consisting solely of whitespace as an empty string.

Conclusion

In this tutorial, we’ve learned to select rows with NULL or empty fields using SQLAlchemy, progressing from basic to advanced techniques. Applying these methods allows for effective data manipulation and retrieval in database-related Python projects.

Next Article: SQLAlchemy: How to store datetime with timezone

Previous Article: SQLAlchemy: Select rows where column is not null

Series: SQLAlchemy Tutorials: From Basic to Advanced

SQLAlchemy

You May Also Like

  • SQLAlchemy: Counting rows for each category (2 approaches)
  • SQLAlchemy: Adding a calculated column to SELECT query
  • SQLAlchemy: Grouping data on multiple columns
  • SQLAlchemy: How to temporarily delete a row
  • SQLAlchemy Composite Indexes: The Complete Guide
  • Full-Text Search in SQLAlchemy: The Ultimate Guide
  • SQLAlchemy: What if you don’t close database connections?
  • SQLAlchemy: How to Remove FOREIGN KEY Constraints (2 Ways)
  • SQLAlchemy: How to Create and Use Temporary Tables
  • SQLAlchemy: Saving Categories and Subcategories in the Same Table
  • SQLAlchemy: How to Automatically Delete Old Records
  • Weighted Random Selection in SQLAlchemy: An In-Depth Guide
  • SQLAlchemy: created_at and updated_at columns
  • How to Use Regular Expressions in SQLAlchemy
  • SQLAlchemy: Ways to Find Results by a Keyword
  • SQLAlchemy: How to Connect to MySQL Database
  • SQLAlchemy: How to Bulk Insert Data into a Table
  • SQLAlchemy: How to Update a Record by ID
  • SQLAlchemy: Singular vs Plural Table Names