123 lines
4.5 KiB
Markdown
123 lines
4.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 or database.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
|
|
|
|
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
|
|
```
|
|
|
|
## 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 or database.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
|
|
|
|
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
|
|
|
|
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):
|
|
1. **PROJECT NAME:** Todo-sovellus
|
|
|
|
2. **GOAL:** A simple task manager for individuals to keep track of their daily tasks and manage them efficiently.
|
|
|
|
3. **CORE FEATURES:**
|
|
- Create new todos with title, description, due date.
|
|
- Read/View all todos or a single todo by ID.
|
|
- Update existing todos (title, description, status).
|
|
- Delete completed or unnecessary todos.
|
|
- Filter todos by status (e.g., pending, completed).
|
|
|
|
4. **DATA MODEL:**
|
|
- Todo
|
|
- id (integer, primary key)
|
|
- title (string)
|
|
- description (text)
|
|
- due_date (date)
|
|
- status (enum: 'pending', 'completed')
|
|
|
|
5. **API ENDPOINTS:**
|
|
- GET /todos: Retrieve all todos.
|
|
- GET /todos/{id}: Retrieve a single todo by ID.
|
|
- POST /todos: Create a new todo.
|
|
- PUT /todos/{id}: Update an existing todo.
|
|
- DELETE /todos/{id}: Delete a todo.
|
|
|
|
6. **CONSTRAINTS:**
|
|
- Must use SQLite as the database.
|
|
- No authentication required for accessing endpoints.
|
|
- Implement unit tests and integration tests to ensure functionality.
|
|
|
|
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, Date, Enum
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
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 Todo(Base):
|
|
__tablename__ = "todos"
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
title = Column(String(100), nullable=False)
|
|
description = Column(String(500))
|
|
due_date = Column(Date, nullable=False)
|
|
status = Column(Enum('pending', 'completed'), default='pending')
|
|
```
|