fix: 添加 25 个真实 FK 约束 + 修复 DELETE/POST /creations 两个 500 bug - #6
Open
liuma3995-dot wants to merge 24 commits into
Open
fix: 添加 25 个真实 FK 约束 + 修复 DELETE/POST /creations 两个 500 bug#6liuma3995-dot wants to merge 24 commits into
liuma3995-dot wants to merge 24 commits into
Conversation
added 24 commits
July 25, 2026 17:27
之前 models/*.py 用 BigInteger 列 + relationship(primaryjoin=...) 模拟外键,
导致 SQLAlchemy 在 db.delete(creation) 时方向推断错乱,抛:
AssertionError: Dependency rule on column 'creations.model_id'
tried to blank-out primary key column 'ai_models.id'
结果是 DELETE /api/v1/creations/{id} 返回 500。
本提交:
- 9 个 models 文件加 ForeignKey(..., ondelete='CASCADE'|'RESTRICT') 真实外键
- 移除 relationship 上的 primaryjoin / remote_side,让 ORM 自动推断
- ai_models.id 由 Integer 改为 BigInteger,与其他主键类型一致
(MySQL 要求 FK 两端整数 size+sign 完全一致)
最终效果:
- 共 25 个外键落到 DB(23 CASCADE + 2 RESTRICT)
- CASCADE:删用户自动清其所有创作/AI模型/平台账号/发布记录/积分流水/...
RESTRICT:删被引用的 ai_models / platform_accounts 被 DB 拒绝(ERROR 1451)
- 删创作自动级联清理 creation_versions / publish_records / plugin_invocations
验证:DELETE 端点 200 + 删 AIModel 时 ERROR 1451
原实现读取 Base.metadata 后手工复制到新 MetaData(), 并在拷贝列时过滤掉所有 ForeignKey 信息 —— 这等于让模型层的 ForeignKey(ondelete=...) 全部失效,DB 永远不会建 FK 约束, docs/DATABASE.md 的 CASCADE/RESTRICT 设计也随之成为空话。 改为直接 Base.metadata.create_all(engine): - 模型声明的外键会原样翻译为 MySQL DDL - 配合上一个 commit 的 models 改动,FK 真正落到数据库 - 加 SET FOREIGN_KEY_CHECKS=0 包裹保证 drop+create 顺序稳定
两个独立的 500 bug:
1. backend/app/api/v1/ai.py:44 / :48
chat_completion 函数签名里有 ChatRequest / ChatResponse 类型注解,
但顶部 import 漏了这两个 Pydantic 类,导致 uvicorn 启动时
NameError: name 'ChatRequest' is not defined,把整个 backend
启动失败。补 import:
from app.schemas.ai_model import ChatRequest, ChatResponse
2. backend/app/api/v1/creations.py:104
create_creation 函数读 creation_data.content_type,但
CreationBase schema 实际字段叫 creation_type,触发:
AttributeError: 'CreationCreate' object has no attribute 'content_type'
这导致前端所有"新建创作"按钮(写作/生图/视频/PPT)点下去就 500。
修复:content_type → creation_type
两个 bug 都通过本地 curl 验证返回 200。
- docs/DATABASE.md 第 286-349 行的「外键约束」段从 5 行扩到完整的
CASCADE / RESTRICT 表格,并新增「DELETE /api/v1/creations/{id}
接口行为」小节描述 4 步删除流程 + 反向 RESTRICT 场景
- frontend/package.json version: 1.0.0 → 1.0.1(PATCH:纯 bug 修复,
无 API 变更)
新增 docs/CHANGELOG.md(Keep a Changelog 格式)。
1.0.1 节记录了两个修复:
1. DELETE /api/v1/creations/{id} 500 修复(FK 缺失)
2. POST /api/v1/creations 500 修复(字段名 typo)
以及两项变更:
- init_db.py 不再剥离 FK
- DATABASE.md 外键约束段扩充
附 1.0.0 → 1.0.1 升级指南:必须 DROP DATABASE 后重建,
否则旧的"无 FK"表与新模型的 FK 声明不一致。
…名单),脚本按开发/本地部署/服务器部署分类整理,新增镜像版交付包、重置密码工具
…密码、会员过期置空、历史标题按工具映射)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复内容
Bug Fixes
DELETE /api/v1/creations/{id}返回 500模型层用
relationship(primaryjoin=...)模拟外键,SQLAlchemy删除时方向错乱,断言失败。改为真实的
ForeignKey(ondelete=...)约束后正常级联清理。
POST /api/v1/creations返回 500creations.py:104引用 schema 里不存在的content_type,实际是
creation_type。后端启动失败
ai.py漏 importChatRequest/ChatResponse,导致 uvicorn 启动时 NameError。Database Changes
init_db.py不再剥离 FK,直接Base.metadata.create_allUpgrade Notes⚠️
mysql -u root -p -e "DROP DATABASE ai_creator; CREATE DATABASE ai_creator CHARACTER SET utf8mb4;" python scripts/init_db.py 现有数据会丢失。保留数据请先 mysqldump 后再重建。 完整说明见 docs/CHANGELOG.md。