SQLAlchemy CIText
raw JSON → 1.8.0 verified Mon Apr 27 auth: no python
A SQLAlchemy plugin that provides CITEXT (case-insensitive text) support for PostgreSQL. The latest version is 1.8.0, with infrequent releases as needed.
pip install sqlalchemy-citext Common errors
error ImportError: cannot import name 'CIText' from 'sqlalchemy_citext' ↓
cause Incorrect installation or missing extension
fix
Reinstall the package: pip install --upgrade sqlalchemy-citext. Ensure you use the correct import: from sqlalchemy_citext import CIText.
error ProgrammingError: type "citext" does not exist ↓
cause CIText type not created in the database
fix
Run: from sqlalchemy_citext import CIText; CIText().create_type(engine). Or let SQLAlchemy create all tables.
Warnings
gotcha CIText creates a custom PostgreSQL type 'citext' on first use. Ensure the user has permissions to CREATE TYPE in the public schema. ↓
fix Grant CREATE ON SCHEMA public TO your_user; or create the type manually before using CIText.
deprecated The import `from sqlalchemy_citext import CIText` is preferred. Older code may use `from citext import CIText` which is deprecated. ↓
fix Use `from sqlalchemy_citext import CIText`.
gotcha CIText only works with PostgreSQL. Attempting to use it with other databases (like SQLite) will cause errors. ↓
fix Ensure your database URL points to a PostgreSQL instance.
Imports
- CIText
from sqlalchemy_citext import CIText - CITextComparator
from sqlalchemy_citext.types import CITextComparator
Quickstart
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_citext import CIText
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(CIText)
engine = create_engine('postgresql://user:pass@localhost/db')
Base.metadata.create_all(engine)
# Insert and query case-insensitively
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
session.add(User(name='Alice'))
session.commit()
user = session.query(User).filter(User.name == 'alice').first() # works
print(user.name) # Alice