Generic Example — Models
This page shows the model layer for a typical entity. For a full end-to-end example (models + schemas + CRUD + APIs + docs), see:
What belongs in models.py
- SQLAlchemy table definitions (columns, constraints, relationships)
- Tenant/company scoping fields (e.g.,
tenant_id/company_id) + indexes - Common timestamps (
created_at,updated_at)
Example models.py
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func, Index
from database.database import Base
class Item(Base):
__tablename__ = "items"
id = Column(String, primary_key=True, index=True)
tenant_id = Column(String, ForeignKey("tenants.id"), nullable=False, index=True)
name = Column(String, nullable=False)
description = Column(String, nullable=True)
price_cents = Column(Integer, nullable=False)
created_at = Column(DateTime, server_default=func.now(), nullable=False)
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), nullable=False)
Index("ix_items_tenant_id_created_at", Item.tenant_id, Item.created_at)
Notes
- Keep names consistent across your project (
tenant_idvscompany_id) and scope every query by that boundary. - Prefer explicit
nullableandindexflags so intent is obvious during review.