mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-04-15 22:26:25 +02:00
114 lines
3.6 KiB
Markdown
114 lines
3.6 KiB
Markdown
# Models
|
|
|
|
!!! info "Added in `v2.0`"
|
|
|
|
Reusable SQLAlchemy 2.0 mixins for common column patterns, designed to be composed freely on any `DeclarativeBase` model.
|
|
|
|
## Overview
|
|
|
|
The `models` module provides mixins that each add a single, well-defined column behaviour. They work with standard SQLAlchemy 2.0 declarative syntax and are fully compatible with `AsyncSession`.
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
|
|
|
class Article(Base, UUIDMixin, TimestampMixin):
|
|
__tablename__ = "articles"
|
|
|
|
title: Mapped[str]
|
|
content: Mapped[str]
|
|
```
|
|
|
|
All timestamp columns are timezone-aware (`TIMESTAMPTZ`). All defaults are server-side, so they are also applied when inserting rows via raw SQL outside the ORM.
|
|
|
|
## Mixins
|
|
|
|
### [`UUIDMixin`](../reference/models.md#fastapi_toolsets.models.UUIDMixin)
|
|
|
|
Adds a `id: UUID` primary key generated server-side by PostgreSQL using `gen_random_uuid()` (requires PostgreSQL 13+). The value is retrieved via `RETURNING` after insert, so it is available on the Python object immediately after `flush()`.
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin
|
|
|
|
class User(Base, UUIDMixin):
|
|
__tablename__ = "users"
|
|
|
|
username: Mapped[str]
|
|
|
|
# id is None before flush
|
|
user = User(username="alice")
|
|
await session.flush()
|
|
print(user.id) # UUID('...')
|
|
```
|
|
|
|
### [`CreatedAtMixin`](../reference/models.md#fastapi_toolsets.models.CreatedAtMixin)
|
|
|
|
Adds a `created_at: datetime` column set to `NOW()` on insert. The column has no `onupdate` hook — it is intentionally immutable after the row is created.
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin, CreatedAtMixin
|
|
|
|
class Order(Base, UUIDMixin, CreatedAtMixin):
|
|
__tablename__ = "orders"
|
|
|
|
total: Mapped[float]
|
|
```
|
|
|
|
### [`UpdatedAtMixin`](../reference/models.md#fastapi_toolsets.models.UpdatedAtMixin)
|
|
|
|
Adds an `updated_at: datetime` column set to `NOW()` on insert and automatically updated to `NOW()` on every ORM-level update (via SQLAlchemy's `onupdate` hook).
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin, UpdatedAtMixin
|
|
|
|
class Post(Base, UUIDMixin, UpdatedAtMixin):
|
|
__tablename__ = "posts"
|
|
|
|
title: Mapped[str]
|
|
|
|
post = Post(title="Hello")
|
|
await session.flush()
|
|
await session.refresh(post)
|
|
|
|
post.title = "Hello World"
|
|
await session.flush()
|
|
await session.refresh(post)
|
|
print(post.updated_at)
|
|
```
|
|
|
|
!!! note
|
|
`updated_at` is updated by SQLAlchemy at ORM flush time. If you update rows via raw SQL (e.g. `UPDATE posts SET ...`), the column will **not** be updated automatically — use a database trigger if you need that guarantee.
|
|
|
|
### [`TimestampMixin`](../reference/models.md#fastapi_toolsets.models.TimestampMixin)
|
|
|
|
Convenience mixin that combines [`CreatedAtMixin`](../reference/models.md#fastapi_toolsets.models.CreatedAtMixin) and [`UpdatedAtMixin`](../reference/models.md#fastapi_toolsets.models.UpdatedAtMixin). Equivalent to inheriting both.
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
|
|
|
class Article(Base, UUIDMixin, TimestampMixin):
|
|
__tablename__ = "articles"
|
|
|
|
title: Mapped[str]
|
|
```
|
|
|
|
## Composing mixins
|
|
|
|
All mixins can be combined in any order. The only constraint is that exactly one primary key must be defined — either via `UUIDMixin` or directly on the model.
|
|
|
|
```python
|
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
|
|
|
class Event(Base, UUIDMixin, TimestampMixin):
|
|
__tablename__ = "events"
|
|
name: Mapped[str]
|
|
|
|
class Counter(Base, UpdatedAtMixin):
|
|
__tablename__ = "counters"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
value: Mapped[int]
|
|
```
|
|
|
|
---
|
|
|
|
[:material-api: API Reference](../reference/models.md)
|