Custom Types¶
Advanced Alchemy provides SQLAlchemy custom types that adapt to different database backends, ensure data integrity, and provide Python-native type annotations.
All custom types include:
Automatic dialect-specific implementations
Python type annotation support
Consistent behavior across database backends
Integration with SQLAlchemy’s type system
Type Categories¶
Category |
Types |
|---|---|
Basic Types |
GUID, DateTimeUTC, JsonB, BigIntIdentity |
Security Types |
EncryptedString, EncryptedText, PasswordHash |
File Storage |
FileObject, FileObjectList, StoredObject |
Collections |
MutableList |
Quick Reference¶
from typing import Optional
from datetime import datetime
from sqlalchemy.orm import Mapped, mapped_column
from advanced_alchemy.base import UUIDAuditBase
from advanced_alchemy.types import (
DateTimeUTC,
JsonB,
EncryptedString,
FileObject,
StoredObject,
)
class User(UUIDAuditBase):
__tablename__ = "users"
# UUID primary key (handled by UUIDAuditBase)
email: "Mapped[str]" = mapped_column(unique=True)
# UTC timezone-aware datetime
last_login: "Mapped[Optional[datetime]]" = mapped_column(DateTimeUTC)
# Efficient JSON storage
preferences: "Mapped[dict]" = mapped_column(JsonB)
# Encrypted password
password: "Mapped[str]" = mapped_column(
EncryptedString(key="encryption-key")
)
# File storage
avatar: "Mapped[Optional[FileObject]]" = mapped_column(
StoredObject(backend="s3")
)
from datetime import datetime
from sqlalchemy.orm import Mapped, mapped_column
from advanced_alchemy.base import UUIDAuditBase
from advanced_alchemy.types import (
DateTimeUTC,
JsonB,
EncryptedString,
FileObject,
StoredObject,
)
class User(UUIDAuditBase):
__tablename__ = "users"
# UUID primary key (handled by UUIDAuditBase)
email: "Mapped[str]" = mapped_column(unique=True)
# UTC timezone-aware datetime
last_login: "Mapped[datetime | None]" = mapped_column(DateTimeUTC)
# Efficient JSON storage
preferences: "Mapped[dict]" = mapped_column(JsonB)
# Encrypted password
password: "Mapped[str]" = mapped_column(
EncryptedString(key="encryption-key")
)
# File storage
avatar: "Mapped[FileObject | None]" = mapped_column(
StoredObject(backend="s3")
)
Database Compatibility¶
Advanced Alchemy custom types work across all supported database backends:
Type |
PostgreSQL |
SQLite |
Oracle |
MySQL |
Others |
|---|---|---|---|---|---|
GUID |
UUID |
BINARY(16) |
RAW(16) |
BINARY(16) |
BINARY(16) |
DateTimeUTC |
TIMESTAMP |
DATETIME |
TIMESTAMP |
DATETIME |
DATETIME |
JsonB |
JSONB |
JSON |
BLOB+CHECK |
JSON |
JSON |
BigIntIdentity |
BIGINT |
INTEGER |
NUMBER(19) |
BIGINT |
BIGINT |
EncryptedString |
VARCHAR |
VARCHAR |
VARCHAR2 |
VARCHAR |
VARCHAR |
FileObject |
JSON/JSONB |
JSON |
BLOB+CHECK |
JSON |
JSON |
Type Selection Guide¶
For UUID primary keys:
Use base classes:
UUIDBase,UUIDAuditBaseManual column:
GUIDtype
For timestamps:
Use base classes with audit columns:
UUIDAuditBase,BigIntAuditBaseManual column:
DateTimeUTCtype
For JSON data:
PostgreSQL/Oracle/CockroachDB:
JsonBuses native binary JSONOther databases: Standard JSON type
For sensitive data:
Passwords:
PasswordHashwith automatic hashingEncrypted fields:
EncryptedStringorEncryptedText
For file storage:
Cloud storage:
StoredObjectwith S3/GCS/Azure backendsLocal storage:
StoredObjectwith local filesystem backend
Detailed Documentation¶
See Also¶
Modeling - Using types in models
Storage Backends - Storage backend configuration
types - API reference for all types