mirror of
https://github.com/himanshudongre/smriti.git
synced 2026-08-28 05:14:59 +00:00
52 lines
2.4 KiB
Text
52 lines
2.4 KiB
Text
Human: I'm building a REST API for a task management app using FastAPI and SQLAlchemy. Can you help me set up the database models? I need a User model with email and name, and a Task model with title, description, status, and a foreign key to the user.
|
|
Assistant: Sure! Here's how I'd structure those models using SQLAlchemy 2.0 with mapped_column:
|
|
|
|
```python
|
|
from sqlalchemy import String, Text, ForeignKey
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
email: Mapped[str] = mapped_column(String(255), unique=True)
|
|
name: Mapped[str] = mapped_column(String(100))
|
|
tasks: Mapped[list["Task"]] = relationship(back_populates="owner")
|
|
|
|
class Task(Base):
|
|
__tablename__ = "tasks"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
title: Mapped[str] = mapped_column(String(200))
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
status: Mapped[str] = mapped_column(String(20), default="pending")
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
|
owner: Mapped["User"] = relationship(back_populates="tasks")
|
|
```
|
|
|
|
We decided to use mapped_column style instead of the older Column() approach since we're on SQLAlchemy 2.0. TODO: add indexes on the email and status columns for query performance.
|
|
Human: Great! What about migrations? Should we use Alembic?
|
|
Assistant: Yes, Alembic is the standard migration tool for SQLAlchemy. We decided to use Alembic with autogenerate. Here's how to set it up:
|
|
|
|
1. Install: `pip install alembic`
|
|
2. Initialize: `alembic init alembic`
|
|
3. Configure `alembic/env.py` to import your Base metadata
|
|
|
|
TODO: create the initial migration after finalizing the models. Should we add soft delete support to the Task model?
|
|
Human: Good question. Let's not add soft delete for the MVP. What about input validation?
|
|
Assistant: For the MVP, I'd recommend using Pydantic for request/response validation. We decided to keep it simple — no soft delete for now, just hard deletes. Here's a basic schema:
|
|
|
|
```python
|
|
from pydantic import BaseModel, EmailStr
|
|
|
|
class UserCreate(BaseModel):
|
|
email: EmailStr
|
|
name: str
|
|
|
|
class TaskCreate(BaseModel):
|
|
title: str
|
|
description: str | None = None
|
|
```
|
|
|
|
TODO: add proper error handling middleware. One open question is whether we should add pagination to the task listing endpoint?
|