other table 中有相关记录且有外键时无法删除记录

Not able to delete records when there are related records in other table with foreign key

收到以下错误,已尝试 onDelete: 'CASCADE' @ManyToOne 关系

[ExceptionsHandler] update or delete on table "resource" violates foreign key constraint "resource_skill_resource_id_fk" on table "resource_skill"

资源实体

import {
    BaseEntity,
    Column,
    Entity,
    Index,
    JoinColumn,
    JoinTable,
    ManyToMany,
    ManyToOne,
    OneToMany,
    OneToOne,
    PrimaryColumn,
    PrimaryGeneratedColumn,
    RelationId
} from 'typeorm';
import { Organization } from './organization';
import { AreaResource } from './area_resource';
import { Client } from './client';
import { EventResource } from './event_resource';
import { LocationResource } from './location_resource';
import { LocationResourceAssignment } from './location_resource_assignment';
import { ResourceSkill } from './resource_skill';
import { TeamResource } from './team_resource';

@Entity('resource', { schema: 'cccalpha1' })
export class Resource {

    @Column('text', {
        nullable: true,
        name: 'first_name'
    })
    first_name: string | null;


    @Column('text', {
        nullable: true,
        name: 'email'
    })
    email: string | null;


    @Column('text', {
        nullable: true,
        name: 'background_color'
    })
    background_color: string | null;


    @Column('text', {
        nullable: true,
        name: 'phone'
    })
    phone: string | null;


    @Column('text', {
        nullable: true,
        name: 'last_name'
    })
    last_name: string | null;


    @Column('text', {
        nullable: true,
        name: 'font_color'
    })
    font_color: string | null;


    @Column('text', {
        nullable: true,
        name: 'full_name'
    })
    full_name: string | null;


    @Column('text', {
        nullable: true,
        name: 'prefix'
    })
    prefix: string | null;


    @Column('text', {
        nullable: true,
        name: 'middle_name'
    })
    middle_name: string | null;


    @Column('text', {
        nullable: true,
        name: 'full_name_last_first'
    })
    full_name_last_first: string | null;


    @Column('boolean', {
        nullable: true,
        name: 'inactive'
    })
    inactive: boolean | null;


    @Column('text', {
        nullable: true,
        name: 'suffix'
    })
    suffix: string | null;


    @Column('text', {
        nullable: true,
        name: 'fax_number'
    })
    fax_number: string | null;


    @Column('boolean', {
        nullable: true,
        name: 'email_schedule_alert'
    })
    email_schedule_alert: boolean | null;


    @Column('text', {
        nullable: true,
        name: 'cell_number'
    })
    cell_number: string | null;


    @Column('boolean', {
        nullable: true,
        name: 'text_schedule_alert'
    })
    text_schedule_alert: boolean | null;


    @Column('text', {
        nullable: true,
        name: 'email_cell_address'
    })
    email_cell_address: string | null;


    @Column('uuid', {
        nullable: false,
        primary: true,
        default: () => 'uuid_generate_v4()',
        name: 'id'
    })
    id: string;


    @ManyToOne(type => Organization, organization => organization.resource, {})
    @JoinColumn({name: 'organization_id'})
    organization: Organization | null;


    @OneToMany(type => AreaResource, area_resource => area_resource.resource, { cascade: true })
    area_resource: AreaResource[];


    @OneToMany(type => Client, client => client.resource)
    client: Client[];


    @OneToMany(type => EventResource, event_resource => event_resource.resource)
    event_resource: EventResource[];


    @OneToMany(type => LocationResource, location_resource => location_resource.resource)
    location_resource: LocationResource[];


    @OneToMany(type => LocationResourceAssignment, location_resource_assignment => location_resource_assignment.resource)
    location_resource_assignment: LocationResourceAssignment[];


    @OneToMany(type => ResourceSkill, resource_skill => resource_skill.resource, { eager: true, cascade: true })
    resource_skill: ResourceSkill[];


    @OneToMany(type => TeamResource, team_resource => team_resource.resource)
    team_resource: TeamResource[];

}

资源技能实体

import {
    BaseEntity,
    Column,
    Entity,
    Index,
    JoinColumn,
    JoinTable,
    ManyToMany,
    ManyToOne,
    OneToMany,
    OneToOne,
    PrimaryColumn,
    PrimaryGeneratedColumn,
    RelationId
} from 'typeorm';
import { Resource } from './resource';
import { Skill } from './skill';

@Entity('resource_skill', { schema: 'cccalpha1' })
export class ResourceSkill {

    @Column('uuid', { 
        nullable: false,
        primary: true,
        default: () => 'uuid_generate_v4()',
        name: 'id'
    })
    id: string;


    @ManyToOne(type => Resource, resource => resource.resource_skill, { onDelete: 'CASCADE' })
    @JoinColumn({ name: 'resource_id' })
    resource: Resource | null;


    @ManyToOne(type => Skill, skill => skill.resource_skill, { onDelete: 'CASCADE' })
    @JoinColumn({ name: 'skill_id' })
    skill: Skill | null;

}

我做了三件事,现在删除效果很好。

  1. 从架构中删除了所有表(不确定是否有必要)
  2. 更改了在 Entity 中声明主列的方式,如下所示,
// Old way
@Column('uuid', { 
   nullable: false,
   primary: true,
   default: () => 'uuid_generate_v4()',
   name: 'id'
})
id: string;

// New way
@PrimaryGeneratedColumn('uuid')
id: string;
  1. 在模块中将synchronize设置为true,使数据库与Entity
  2. 同步

synchronize - Synchronizes database schema. When synchronize: true is set in connection options it calls this method. Usually, you call this method when your application is shutting down.

P.S:可能第一步不是必需的,但我已经在删除表格的路径上了

同步:true => 发挥了作用