我有以下实体.如果使用类别存储库的删除或移除删除父类别,我如何自动删除子类别?
import { UserEntity } from '@entities/user.entity';
import { Entity, Column, ManyToOne, PrimaryGeneratedColumn, Relation, OneToMany } from 'typeorm';
@Entity()
export class CategoryEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: false, unique: true })
name: string;
@Column({ nullable: false })
icon: string;
@ManyToOne(Type => UserEntity, user => user.categories)
user: Relation<UserEntity>;
@ManyToOne(type => CategoryEntity, category => category.childCategories, {
createForeignKeyConstraints: false,
})
parentCategory: CategoryEntity;
@OneToMany(type => CategoryEntity, category => category.parentCategory)
childCategories: CategoryEntity[];
}
实际上,我试着按id删除类别,类别被删除了,但它的子数据仍然保留在数据库中.