SQLite
The SQLite provider uses SQLAlchemy with
Python's built-in sqlite3 module to provide a file-based relational database.
Overview
SQLite is a good choice when you need real SQL semantics without running a separate database server. It is useful for:
- Local development with real transactions and SQL queries
- Testing when you need ACID guarantees but want to avoid Docker services
- Single-user applications or embedded scenarios
Because SQLite is part of the Python standard library, no additional packages are needed beyond Protean's SQLAlchemy dependency.
Installation
SQLite support is included with Protean's SQLAlchemy integration. If SQLAlchemy is installed, SQLite is available automatically:
pip install "protean[postgresql]" # Includes SQLAlchemy
# -- or --
pip install sqlalchemy
Configuration
[databases.default]
provider = "sqlite"
database_uri = "${DATABASE_URL|sqlite:///app.db}"
${DATABASE_URL|...} reads the URI from the DATABASE_URL environment
variable, and uses the value after | when it is not set.
Configuration Options
| Option | Default | Description |
|---|---|---|
provider |
Required | Must be "sqlite" for the SQLite provider |
database_uri |
Required | SQLite connection string |
Connection String Formats
sqlite:///relative/path/to/file.db # Relative path
sqlite:////absolute/path/to/file.db # Absolute path (note four slashes)
sqlite:// # In-memory SQLite (lost on close)
sqlite:///test.db # File in current directory
Capabilities
The SQLite provider supports the following capabilities:
CRUD: Create, Read, Update, Delete single records
FILTER: Query/filter records with lookup criteria
BULK_OPERATIONS:
update_all(),delete_all()ORDERING: Server-side
ORDER BYsupportTRANSACTIONS: Real commit/rollback atomicity
OPTIMISTIC_LOCKING: Version-based concurrency control
RAW_QUERIES: Execute raw SQL queries
SCHEMA_MANAGEMENT: Create/drop tables
CONNECTION_POOLING: SQLAlchemy connection pool management
SCHEMA_MIGRATIONS: Versioned migrations through Alembic
SIMULATED_TRANSACTIONS: Uses real transactions instead
NATIVE_JSON: No native JSON column type (stored as text)
NATIVE_ARRAY: No native array column type (stored as text)
BOUNDED_DELETE: Deletes up to
limitrows in one operation, without a separate read of their idsFILTERED_CLAIM_CANDIDATES: Leaves held rows out of a claim's candidates
NATIVE_CLAIM: Claims rows in one statement
INSERT_IF_ABSENT: Writes a row only when no other row holds its unique key, in one atomic step
Timestamps
A DateTime field maps to a DATETIME column, which SQLite stores as text.
A DateTime column holds no UTC offset, so Protean converts each value to UTC
for writes and filter comparisons. Protean reads a naive value as UTC. A load
returns a UTC-aware datetime. A filter value in any offset then matches the
same instant. Rows written before 0.18.1 hold the wall-clock time
they were saved with; see
Datetimes are compared and stored as UTC
to convert them.
Indexes
protean db setup creates SQLite indexes with most of the
Index options:
- Composite, descending (
desc=), and unique (unique=) indexes. - Partial indexes (
where=Q(...)). - Covering columns (
include=) are not supported; the index is created without them and a warning is logged.
SQLAlchemy Model
You can supply a custom SQLAlchemy Model to replace the one Protean generates internally. A custom model gives you full control of column types and constraints. The pattern is identical to PostgreSQL models.
# fragment
import sqlalchemy as sa
from protean import Domain
from protean.fields import String
domain = Domain(name="Accounts")
domain.config["databases"]["default"] = {
"provider": "sqlite",
"database_uri": "sqlite:///test.db",
}
@domain.aggregate
class User:
name: String(max_length=100)
email: String(max_length=255)
@domain.database_model(part_of=User)
class UserModel:
name = sa.Column(sa.String(100))
email = sa.Column(sa.String(255), unique=True)
domain.init(traverse=False)
Note
The column names specified in the model should exactly match the attribute names of the Aggregate or Entity it represents.
Slow Query Detection
The SQLite provider is built on SQLAlchemy, so it emits the same structured log
events as the PostgreSQL provider.
These are the protean.adapters.database.sqlalchemy.slow_query WARNING and
the protean.adapters.database.sqlalchemy.query DEBUG events. Configure
the threshold via [logging].slow_query_threshold_ms in domain.toml.
Limitations
- Single-Writer Concurrency: SQLite supports only one writer at a time. Concurrent writes from multiple processes will block or fail.
- No Native JSON/Array: Complex fields (JSON, arrays) are stored as serialized text. Queries cannot filter on nested JSON properties.
- Not Suitable for Multi-Process Production: SQLite is designed for single-process use. Use PostgreSQL for production deployments.
- File Locking: The database file must be on a local filesystem. Network filesystems (NFS, SMB) are not reliable with SQLite.
Related pages
- Learn about PostgreSQL for production environments
- Understand database capabilities in detail
- Learn about custom SQLAlchemy models