mysql - TypeORM/MySQL:无法删除或更新父行:外键约束失败
问题描述
comments
我在和之间有实体关系posts
_many-to-one_
。我正在使用typeorm
和typegraphql
这是我的帖子实体:
@ObjectType()
@Entity()
export class Post extends BaseEntity {
constructor(input: InputI) {
super();
this.caption = input?.caption;
this.imageURL = input?.imageURL;
this.status = input?.status;
this.user = input?.user;
}
@Field(() => Int)
@PrimaryGeneratedColumn({ type: "int" })
id: number;
@Field(() => String, { nullable: true })
@Column({ type: "text", nullable: true })
caption?: string;
@Field(() => String, { nullable: true })
@Column({ type: "text", nullable: true })
imageURL?: string;
@Field(() => String, { nullable: true })
@Column({ type: "text", nullable: true })
status?: string;
// Relations
@Field(() => User)
@ManyToOne(() => User, (user) => user.posts)
user: User;
@Field(() => [Comments], { nullable: true })
@OneToMany(() => Comments, (comment) => comment.post, {
onDelete: "CASCADE",
onUpdate: "CASCADE",
})
comments: Comments[];
@Field(() => [Likes], { nullable: true })
@OneToMany(() => Likes, (like) => like.post, {
onDelete: "CASCADE",
onUpdate: "CASCADE",
})
likes: Likes[];
@Field(() => String)
@CreateDateColumn({ nullable: false })
createdAt: Date;
@Field(() => String)
@UpdateDateColumn({ nullable: false })
updatedAt: Date;
}
这是我的评论实体:
@ObjectType()
@Entity()
export class Comments extends BaseEntity {
@Field(() => Int)
@PrimaryGeneratedColumn({ type: "int" })
id: number;
@Field(() => String)
@Column({ nullable: true })
avatar: string;
@Field(() => String)
@Column({ nullable: false })
comment: string;
@Field(() => String)
@Column({ nullable: false })
email: string;
@Field(() => String)
@Column({ nullable: false })
username: string;
@Field(() => String)
@Column({ nullable: true })
phoneNumber: string;
@Field(() => String)
@Column({ nullable: false })
gender: string;
@Field(() => String)
@Column({ nullable: false })
status: string;
@Field(() => String)
@Column({ nullable: true, type: "text" })
bio: string;
@Field(() => Boolean)
@Column({ nullable: false, default: false })
verified: false | true;
// @Field(() => [Comments], { nullable: true })
// @OneToMany(() => Comments, (comment) => comment)
// @JoinColumn()
// replies: Comments[];
// @ManyToOne(() => Post, (post) => post.likes)
// post: Post;
@ManyToOne(() => Post, (post) => post.comments)
post: Post;
//
@Field(() => String)
@CreateDateColumn({ type: "datetime", nullable: false })
createdAt: Date;
@Field(() => String)
@UpdateDateColumn({ type: "datetime", nullable: false })
updatedAt: Date;
}
我对其他突变没有任何问题,唯一的问题是当我想删除帖子时。我在 GraphQLPlayGround 中收到以下错误:
"errors": [
{
"message": "Cannot delete or update a parent row: a foreign key constraint fails (`likeme`.`comments`, CONSTRAINT `FK_e44ddaaa6d058cb4092f83ad61f` FOREIGN KEY (`postId`) REFERENCES `post` (`id`))",
...
}
这是我删除帖子的解析器。
@Resolver()
export class DeletePostResolver {
@Mutation(() => Boolean)
async deletePost(
@Arg("id", () => Int) id: number,
@Ctx() { req }: UserContext
): Promise<Boolean> {
const post = await Post.findOne(
{ id },
{ relations: ["user", "comments", "likes"] }
);
if (post?.user.id === req.session.userId) {
await Post.delete({ id });
return true;
}
return false;
}
}
什么可能是我的问题,我已经设置了onDelete
,CASCADE
所以这里有什么问题?请帮忙。
解决方案
您需要onDelete
在Comment
实体中为 column设置post
。删除Post
实体时,实体中的post
列Comment
需要解析关系引用,但onDelete
没有为post
列设置,查询无法运行。
评论实体:
@ObjectType()
@Entity()
export class Comments extends BaseEntity {
@Field(() => Int)
@PrimaryGeneratedColumn({ type: "int" })
id: number;
@Field(() => String)
@Column({ nullable: true })
avatar: string;
@Field(() => String)
@Column({ nullable: false })
comment: string;
@Field(() => String)
@Column({ nullable: false })
email: string;
@Field(() => String)
@Column({ nullable: false })
username: string;
@Field(() => String)
@Column({ nullable: true })
phoneNumber: string;
@Field(() => String)
@Column({ nullable: false })
gender: string;
@Field(() => String)
@Column({ nullable: false })
status: string;
@Field(() => String)
@Column({ nullable: true, type: "text" })
bio: string;
@Field(() => Boolean)
@Column({ nullable: false, default: false })
verified: false | true;
// @Field(() => [Comments], { nullable: true })
// @OneToMany(() => Comments, (comment) => comment)
// @JoinColumn()
// replies: Comments[];
// @ManyToOne(() => Post, (post) => post.likes)
// post: Post;
@ManyToOne(() => Post, (post) => post.comments, {
onDelete: "CASCADE", // <---- HERE
})
post: Post;
//
@Field(() => String)
@CreateDateColumn({ type: "datetime", nullable: false })
createdAt: Date;
@Field(() => String)
@UpdateDateColumn({ type: "datetime", nullable: false })
updatedAt: Date;
}
推荐阅读
- javascript - 基于firebase身份验证限制Javascript中的路由
- .htaccess - 删除 .htaccess 中的 .php 不起作用
- svg - SVG 动画 - 如何在其父级围绕中心点旋转时保持文本水平
- html - 在某些设备上更新网站,但在其他设备上不更新
- visual-studio-code - 如何在 VSCode 中使用旧的(es6 之前的)代码片段
- java - Retrieve OBJECT from a navigation View (drawer) dynamically android/java
- sql - SQL Server 累计总数
- node.js - AWS 与 Firebase [内容交付网络]
- ruby-on-rails - Rails 5.2 App 推送到 Heroku 成功。Heroku 打开失败。
- reactjs - Allowing CORS from React front end not allowing Express back end to use Bing API