fix(topics): 外键约束导致 delete 400
选题有关联 content_tasks/article/publish_record/metrics 表, 外键约束阻止删除。改为先级联删除关联数据再删选题。 前端同时显示服务端返回的具体错误信息,而不是笼统的 删除失败。
This commit is contained in:
@@ -189,13 +189,19 @@ def delete_topic(
|
|||||||
if not topic:
|
if not topic:
|
||||||
raise HTTPException(status_code=404, detail="Topic not found")
|
raise HTTPException(status_code=404, detail="Topic not found")
|
||||||
_check_org(topic, current_user, db)
|
_check_org(topic, current_user, db)
|
||||||
|
from ..models import ContentTask, Article, PublishRecord, ContentMetrics, ContentCalendar
|
||||||
try:
|
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.delete(topic)
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.rollback()
|
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")
|
@router.post("/{topic_id}/score")
|
||||||
|
|||||||
@@ -547,12 +547,16 @@ const TopicsApp = {
|
|||||||
},
|
},
|
||||||
async deleteTopic(id) {
|
async deleteTopic(id) {
|
||||||
try {
|
try {
|
||||||
await this.$confirm('确定删除?', '提示', { type: 'warning' });
|
await this.$confirm('确定删除选题 ' + id + '?将同时删除相关文章、发布记录等关联数据。', '提示', { type: 'warning' });
|
||||||
await this.api('/api/topics/' + id, { method: 'DELETE' });
|
await this.api('/api/topics/' + id, { method: 'DELETE' });
|
||||||
this.$message.success('删除成功');
|
this.$message.success('删除成功');
|
||||||
await this.fetchTopics();
|
await this.fetchTopics();
|
||||||
await this.fetchTodayCount();
|
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) {
|
switchPlatform(platform) {
|
||||||
if (this.editing) this.cancelEdit();
|
if (this.editing) this.cancelEdit();
|
||||||
|
|||||||
Reference in New Issue
Block a user