Migrate Todo CRUD to SQLAlchemy ORM

This commit is contained in:
2026-07-21 16:11:23 +08:00
parent a54364f8f5
commit b50625c606
6 changed files with 207 additions and 118 deletions
+7 -4
View File
@@ -1,6 +1,6 @@
# TodoList 全栈项目(FastAPI + MySQL)
这是一个用于学习后端开发的 TodoList 项目。前端由 Nginx 提供,`/api` 请求反向代理到 FastAPI,FastAPI 使用参数化 SQL 读写 Docker 中的 MySQL 8.4。
这是一个用于学习后端开发的 TodoList 项目。前端由 Nginx 提供,`/api` 请求反向代理到 FastAPI,FastAPI 使用 SQLAlchemy 2.x ORM 和 PyMySQL 驱动读写 Docker 中的 MySQL 8.4。
## 架构
@@ -15,7 +15,9 @@
## 目录
```text
main.py FastAPI 后端
main.py FastAPI 路由和接口逻辑
database.py 数据库 URL、Engine 和 ORM Base
models.py SQLAlchemy ORM 数据模型
frontend/index.html 前端页面
compose.yaml MySQL Docker Compose 配置
schema.sql 数据库建表脚本
@@ -119,7 +121,7 @@ curl http://127.0.0.1/
curl http://127.0.0.1/api/todos
```
当前测试环境的 Windows 局域网入口为 `http://192.168.10.55:8080`。该地址属于环境配置,不应视为固定生产地址。
Windows 局域网入口为 `http://<Windows-LAN-IP>:8080`。DHCP 或网络切换可能改变该地址,因此不应将它视为固定生产地址。
## API
@@ -135,6 +137,7 @@ Nginx 对外使用 `/api` 前缀,例如浏览器访问 `/api/todos`,代理
## 安全说明
- 所有带用户数据的 SQL 均使用参数化查询,不使用 f-string 拼接。
- CRUD 接口通过 SQLAlchemy ORM 操作数据库;SQLAlchemy 使用绑定参数传递用户数据,不使用 f-string 拼接 SQL。
- 数据库密码通过 `.env` 读取,并使用 `URL.create()` 构建连接地址,不写死在 Python 代码中。
- `.env` 和 MySQL 数据卷不在交付包内。
- 交付包中的 `.env.example` 只有占位符。