fix(topics): 外键约束导致 delete 400

选题有关联 content_tasks/article/publish_record/metrics 表,
外键约束阻止删除。改为先级联删除关联数据再删选题。

前端同时显示服务端返回的具体错误信息,而不是笼统的 删除失败。
This commit is contained in:
Yuzhiran Dev
2026-06-01 12:08:51 +08:00
parent 468c9066cb
commit 1084388fec
2 changed files with 13 additions and 3 deletions
+7 -1
View File
@@ -189,13 +189,19 @@ def delete_topic(
if not topic:
raise HTTPException(status_code=404, detail="Topic not found")
_check_org(topic, current_user, db)
from ..models import ContentTask, Article, PublishRecord, ContentMetrics, ContentCalendar
try:
db.query(ContentCalendar).filter(ContentCalendar.topic_id == topic_id).delete()
db.query(ContentTask).filter(ContentTask.topic_id == topic_id).delete()
db.query(Article).filter(Article.topic_id == topic_id).delete()
db.query(PublishRecord).filter(PublishRecord.topic_id == topic_id).delete()
db.query(ContentMetrics).filter(ContentMetrics.topic_id == topic_id).delete()
db.delete(topic)
db.commit()
return {"ok": True}
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=f"删除失败:该选题有关联数据(文章/发布记录等),请先删除关联数据。{str(e)}")
raise HTTPException(status_code=400, detail=f"删除失败:{str(e)}")
@router.post("/{topic_id}/score")
+6 -2
View File
@@ -547,12 +547,16 @@ const TopicsApp = {
},
async deleteTopic(id) {
try {
await this.$confirm('确定删除', '提示', { type: 'warning' });
await this.$confirm('确定删除选题 ' + id + '?将同时删除相关文章、发布记录等关联数据。', '提示', { type: 'warning' });
await this.api('/api/topics/' + id, { method: 'DELETE' });
this.$message.success('删除成功');
await this.fetchTopics();
await this.fetchTodayCount();
} catch (e) { if (e !== 'cancel') this.$message.error('删除失败'); }
} catch (e) {
if (e === 'cancel') return;
const msg = e?.response?.data?.detail || e?.message || '未知错误';
this.$message.error('删除失败: ' + msg);
}
},
switchPlatform(platform) {
if (this.editing) this.cancelEdit();