142 lines
5.5 KiB
Markdown
142 lines
5.5 KiB
Markdown
# 1 — Data Engineer (data) — models.py
|
|
|
|
**Malli:** `qwen-coder`
|
|
|
|
## System Prompt
|
|
|
|
```
|
|
You are a database architect specializing in SQLAlchemy and relational databases.
|
|
|
|
YOUR RESPONSIBILITIES:
|
|
1. Design normalized database schemas with proper column types and constraints
|
|
2. Define SQLAlchemy models with __tablename__, primary keys, indexes, and relationships
|
|
3. Set up engine, SessionLocal, and Base in the same file (models.py)
|
|
4. Use String(length) not bare String for SQLite compatibility
|
|
5. Add nullable=False for required fields, unique=True where appropriate
|
|
6. Use Column(Integer, primary_key=True, index=True) for IDs
|
|
7. SQLite: create_engine(url, connect_args={"check_same_thread": False})
|
|
|
|
ENUM HANDLING (IMPORTANT):
|
|
- For status fields, use Column(String(20)) with a default value — simpler and SQLite-compatible
|
|
- Do NOT define Python Enum classes — use plain strings instead
|
|
- Example: status = Column(String(20), default="pending")
|
|
|
|
ALWAYS INCLUDE:
|
|
- from sqlalchemy import create_engine, Column, Integer, String
|
|
- from sqlalchemy.ext.declarative import declarative_base
|
|
- from sqlalchemy.orm import sessionmaker
|
|
- DATABASE_URL, engine, SessionLocal, Base
|
|
- create_engine with connect_args={"check_same_thread": False}
|
|
```
|
|
|
|
## Syöte
|
|
|
|
```
|
|
You are a database architect specializing in SQLAlchemy and relational databases.
|
|
|
|
YOUR RESPONSIBILITIES:
|
|
1. Design normalized database schemas with proper column types and constraints
|
|
2. Define SQLAlchemy models with __tablename__, primary keys, indexes, and relationships
|
|
3. Set up engine, SessionLocal, and Base in the same file (models.py)
|
|
4. Use String(length) not bare String for SQLite compatibility
|
|
5. Add nullable=False for required fields, unique=True where appropriate
|
|
6. Use Column(Integer, primary_key=True, index=True) for IDs
|
|
7. SQLite: create_engine(url, connect_args={"check_same_thread": False})
|
|
|
|
ENUM HANDLING (IMPORTANT):
|
|
- For status fields, use Column(String(20)) with a default value — simpler and SQLite-compatible
|
|
- Do NOT define Python Enum classes — use plain strings instead
|
|
- Example: status = Column(String(20), default="pending")
|
|
|
|
ALWAYS INCLUDE:
|
|
- from sqlalchemy import create_engine, Column, Integer, String
|
|
- from sqlalchemy.ext.declarative import declarative_base
|
|
- from sqlalchemy.orm import sessionmaker
|
|
- DATABASE_URL, engine, SessionLocal, Base
|
|
- create_engine with connect_args={"check_same_thread": False}
|
|
|
|
EXAMPLE of models.py (for a different project, adapt to this one):
|
|
```
|
|
from sqlalchemy import create_engine, Column, Integer, String
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
DATABASE_URL = "sqlite:///./app.db"
|
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
class Item(Base):
|
|
__tablename__ = "items"
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String(100), nullable=False)
|
|
description = Column(String(500))
|
|
```
|
|
|
|
PROJECT REQUIREMENTS (from product owner):
|
|
### PROJECT NAME: Todo-App
|
|
|
|
### GOAL:
|
|
Create a simple task manager application for individuals to keep track of their daily tasks.
|
|
|
|
### CORE FEATURES:
|
|
1. **Add Task**: Users can add new tasks with title and description.
|
|
2. **View Tasks**: Users can see all their tasks in a list.
|
|
3. **Edit Task**: Users can update the details of an existing task.
|
|
4. **Delete Task**: Users can remove tasks they no longer need.
|
|
5. **Mark as Complete**: Users can mark tasks as completed.
|
|
6. **Filter Tasks**: Users can filter tasks by status (e.g., pending, completed).
|
|
7. **Search Tasks**: Users can search for specific tasks using keywords in the title or description.
|
|
|
|
### DATA MODEL:
|
|
- **Tasks**
|
|
- `id` (integer, primary key)
|
|
- `title` (string, required)
|
|
- `description` (text)
|
|
- `status` (enum: "pending", "completed")
|
|
- `created_at` (timestamp)
|
|
|
|
### API ENDPOINTS:
|
|
1. **GET /tasks**: Retrieve a list of all tasks.
|
|
2. **POST /tasks**: Create a new task with title, description, and status.
|
|
3. **PUT /tasks/{id}**: Update an existing task by ID.
|
|
4. **DELETE /tasks/{id}**: Delete a task by ID.
|
|
5. **GET /tasks/status/{status}**: Retrieve tasks filtered by status.
|
|
|
|
### CONSTRAINTS:
|
|
- Must use SQLite as the database.
|
|
- No authentication required for simplicity.
|
|
|
|
NOW write "models.py" for THIS project: Todo-sovellus FastAPI + SQLite, CRUD-endpointit ja testit
|
|
Define the SQLAlchemy model based on the project description. Always include:
|
|
- engine with check_same_thread=False for SQLite
|
|
- SessionLocal with autocommit=False
|
|
- Base = declarative_base()
|
|
- Model class with __tablename__, primary key, and fields
|
|
Adapt to the project requirements. Import from already written files. Write ONLY the code, no explanations.
|
|
```
|
|
|
|
## Tulos
|
|
|
|
```
|
|
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from datetime import datetime
|
|
|
|
DATABASE_URL = "sqlite:///./todo.db"
|
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
class Task(Base):
|
|
__tablename__ = "tasks"
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
title = Column(String(100), nullable=False)
|
|
description = Column(Text)
|
|
status = Column(String(20), default="pending")
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
```
|