【发布时间】:2018-08-28 00:47:59
【问题描述】:
删除我的实体时遇到问题,我有以下部分代码
@ManyToOne(type => Comment, comment => comment.replies, {
onDelete: "CASCADE"
})
parent: Comment;
和
@OneToMany(type => Comment, comment => comment.parent)
replies: Comment[];
我已经尝试过manager.remove(comment) 和
await manager
.createQueryBuilder()
.delete()
.from(Comment)
.where("id = :id", { id: comment.id })
.execute();
两者都不起作用,是不是有什么问题,我该怎么做,这是我的选择查询
let comment = await manager
.createQueryBuilder(Comment, "comment")
.leftJoinAndSelect("comment.user", "user")
.where("comment.id = :id", { id: request.body.comment })
.getOne();
得到的错误是
UnhandledPromiseRejectionWarning: QueryFailedError: ER_ROW_IS_REFERENCED_2: Cannot delete or update a parent row: a foreign key constraint fails
提前致谢。
【问题讨论】:
-
尽管这个问题的措辞不是很好,但您添加的代码示例以及它最终起作用的事实帮助我弄清楚了如何正确配置级联操作。谢谢!
标签: node.js typescript typeorm