当其他表中有外键相关记录时,无法删除记录

时间:2019-07-17 18:12:24

标签: javascript postgresql nestjs typeorm

遇到以下错误,尝试对sqrt_button = Button(okno, text='√', fg=color_fg, bg="snow", command=lambda: press(sqrt), height=h, width=w, ) sqrt_button.grid(row=7, column=2) okno.bind("a", lambda event: press(sqrt) ) procent = Button(okno, text="%", fg=color_fg, bg=color_bg, command=lambda: press("/100*"), height=h, widt=w) procent.grid(row=7,column=0) okno.bind("%", lambda evnt: press("/100*")) def press(num): global wyrazenie wyrazenie = wyrazenie + str(num) equation.set(wyrazenie) def equalpress(): try: global wyrazenie total = str(eval(wyrazenie)) equation.set(total) wyrazenie = "" except: equation.set(" błąd ") wyrazenie = "" 关系进行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[];

}

2 个答案:

答案 0 :(得分:0)

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

  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同步
  

synchronize-同步数据库架构。同步时:在连接选项中设置为true时,它将调用此方法。通常,您在应用程序关闭时调用此方法。

P.S:也许第一步不是必需的,但我已经走到放置桌子的路径上

答案 1 :(得分:0)

同步:true =>做了魔术