From 1084388fecf0abf89349f8cab736131307791952 Mon Sep 17 00:00:00 2001 From: Yuzhiran Dev Date: Mon, 1 Jun 2026 12:08:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(topics):=20=E5=A4=96=E9=94=AE=E7=BA=A6?= =?UTF-8?q?=E6=9D=9F=E5=AF=BC=E8=87=B4=20delete=20400?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 选题有关联 content_tasks/article/publish_record/metrics 表, 外键约束阻止删除。改为先级联删除关联数据再删选题。 前端同时显示服务端返回的具体错误信息,而不是笼统的 删除失败。 --- platform/backend/app/api/topics.py | 8 +++++++- platform/frontend/topics.html | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/platform/backend/app/api/topics.py b/platform/backend/app/api/topics.py index 50c4067..dad7e35 100644 --- a/platform/backend/app/api/topics.py +++ b/platform/backend/app/api/topics.py @@ -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") diff --git a/platform/frontend/topics.html b/platform/frontend/topics.html index 66c3428..09b6482 100644 --- a/platform/frontend/topics.html +++ b/platform/frontend/topics.html @@ -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();