profiles.json: role-kenttä (primary/backup/minimal/retired). code-small.md: lisätty konkreettinen FK-pattern ja testi-esimerkki relaatioille — 8b:n blog-skenaario kaatui koska ei osannut FK:ta.
51 lines
1.7 KiB
Markdown
51 lines
1.7 KiB
Markdown
Generate a FastAPI project with SQLAlchemy and SQLite. Follow the REFERENCE IMPLEMENTATION exactly.
|
|
|
|
Generate these 4 files with === markers:
|
|
|
|
=== models.py ===
|
|
=== schemas.py ===
|
|
=== main.py ===
|
|
=== test_main.py ===
|
|
|
|
Key patterns (copy from reference):
|
|
- class Base(DeclarativeBase): pass
|
|
- Mapped[str] = mapped_column(String(255))
|
|
- Mapped[str | None] = mapped_column(Text, default=None)
|
|
- model_config = ConfigDict(from_attributes=True)
|
|
- model_dump() not dict()
|
|
- POST 201, GET list, GET by id 404, PUT, DELETE 204
|
|
|
|
FOREIGN KEYS (when spec has relationships):
|
|
- Child entity gets parent_id field: Mapped[int] = mapped_column(ForeignKey("parents.id"))
|
|
- Import ForeignKey: from sqlalchemy import ForeignKey
|
|
- Create schema includes parent_id: int
|
|
- Test creates parent FIRST, then child with parent's id
|
|
|
|
Example FK pattern in models.py:
|
|
```
|
|
class Author(Base):
|
|
__tablename__ = "authors"
|
|
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
|
name: Mapped[str] = mapped_column(String(255))
|
|
|
|
class Post(Base):
|
|
__tablename__ = "posts"
|
|
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
|
title: Mapped[str] = mapped_column(String(255))
|
|
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"))
|
|
```
|
|
|
|
Example FK test pattern:
|
|
```
|
|
def test_create_post():
|
|
author = client.post("/authors/", json={"name": "Kirjailija"}).json()
|
|
response = client.post("/posts/", json={"title": "Artikkeli", "author_id": author["id"]})
|
|
assert response.status_code == 201
|
|
```
|
|
|
|
CRITICAL:
|
|
- Use ONLY fields from the JSON spec — no created_at or extra fields
|
|
- Generate EXACTLY 6 tests per entity: create, list, get_by_id, not_found, update, delete
|
|
- No search, filter, or other extra tests
|
|
- No markdown fences in output
|