Typeorm 级联删除 [英] Typeorm Cascade Delete

查看:182
本文介绍了Typeorm 级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在删除我的实体时遇到问题,我有以下部分代码

@ManyToOne(type => Comment, comment => comment.replies, {onDelete: "级联"})家长:评论;

@OneToMany(type => Comment, comment => comment.parent)回复:评论[];

我已经尝试过 manager.remove(comment)

等待管理器.createQueryBuilder().删除().from(评论).where("id = :id", { id: comment.id }).执行();

两者都不起作用,是不是有什么地方做错了,我该怎么做,这是我的选择查询

let comment = await manager.createQueryBuilder(评论,评论").leftJoinAndSelect("comment.user", "user").where("comment.id = :id", { id: request.body.comment }).得到一个();

我得到的错误是

UnhandledPromiseRejectionWarning:QueryFailedError:ER_ROW_IS_REFERENCED_2:无法删除或更新父行:外键约束失败

提前致谢.

解决方案

显然我必须删除所有表并为 onDelete: "CASCADE" 重新迁移才能生效但有效>

Am having troubles deleting my entities, i have the following partial codes

@ManyToOne(type => Comment, comment => comment.replies, {
    onDelete: "CASCADE"
})
parent: Comment;

and

@OneToMany(type => Comment, comment => comment.parent)
replies: Comment[];

I have tried manager.remove(comment) and

await manager
    .createQueryBuilder()
    .delete()
    .from(Comment)
    .where("id = :id", { id: comment.id })
    .execute();

Both don't work, is there something am doing wrong, how do I go about this, here is my select query

let comment = await manager
    .createQueryBuilder(Comment, "comment")
    .leftJoinAndSelect("comment.user", "user")
    .where("comment.id = :id", { id: request.body.comment })
    .getOne();

The error am getting is

UnhandledPromiseRejectionWarning: QueryFailedError: ER_ROW_IS_REFERENCED_2: Cannot delete or update a parent row: a foreign key constraint fails

Thanks in advance.

解决方案

Apparently i had to delete all tables and do a fresh migration for the onDelete: "CASCADE" to take effect but worked

这篇关于Typeorm 级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆