postgresql - 外键约束问题

我正在开发一个创建 Ideas api 的项目,类似于论坛,人们可以在其中发布想法,其他人可以发表评论、赞成票和反对票。

我的数据库出现问题,我不知道如何解决。

错误是:"update or delete on table\"idea\"violates foreign key constraint\"FK_861b419cce1c9ae64295300d6b6\"on table\"comment\"

所以你们可以在错误中看到,我有一个名为“idea”的表,我在其中放置了想法和评论。

但是当我尝试删除附有一些评论的 Idea 时会发生此错误。

如果我在没有任何评论的情况下删除这个想法,一切都会好起来的。

但是,如果该想法附有评论,则会出现此错误。

我在网上搜索了一下,有些人让我把我的关系放在@OneToMany(一个想法,很多评论){ cascade: true } 上,但什么也没发生。

我将发布我的实体是如何组织的。

澄清一下,我使用 NestJS 框架和 PostgreSQL 作为关系数据库


import {
  Entity,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  Column,
  ManyToOne,
  JoinTable,
} from 'typeorm';
import { UserEntity } from 'src/user/user.entity';
import { IdeaEntity } from 'src/idea/idea.entity';

@Entity('comment')
export class CommentEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @CreateDateColumn()
  created: Date;

  @Column('text')
  comment: string;

  @ManyToOne(type => UserEntity)
  @JoinTable()
  author: UserEntity;

  @ManyToOne(
    type => IdeaEntity,
    idea => idea.comments,
  )
  idea: IdeaEntity;
}

import {
  Entity,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  Column,
  ManyToOne,
  UpdateDateColumn,
  ManyToMany,
  JoinTable,
  OneToMany,
} from 'typeorm';
import { UserEntity } from 'src/user/user.entity';
import { CommentEntity } from 'src/comment/comment.entity';

@Entity('idea')
export class IdeaEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @CreateDateColumn()
  created: Date;

  @UpdateDateColumn()
  updated: Date;

  @Column('text')
  idea: string;

  @Column('text')
  description: string;

  @ManyToOne(
    type => UserEntity,
    author => author.ideas,
  )
  author: UserEntity;

  @ManyToMany(type => UserEntity, { cascade: true })
  @JoinTable()
  upvotes: UserEntity[];

  @ManyToMany(type => UserEntity, { cascade: true })
  @JoinTable()
  downvotes: UserEntity[];

  @OneToMany(
    type => CommentEntity,
    comment => comment.idea,
    { cascade: true },
  )
  comments: CommentEntity[];
}

edit1:我捕获了所有内容,以查看有关此错误的更多信息,这就是我的终端显示的内容

error: error: update or delete on table "idea" violates foreign key constraint "FK_861b419cce1c9ae64295300d6b6" on table "comment"
    at Connection.parseE (/home/gabriel/Desktop/Token/ideas-api/node_modules/pg/lib/connection.js:614:13)
    at Connection.parseMessage (/home/gabriel/Desktop/Token/ideas-api/node_modules/pg/lib/connection.js:413:19)
    at Socket.<anonymous> (/home/gabriel/Desktop/Token/ideas-api/node_modules/pg/lib/connection.js:129:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9)
    at Socket.Readable.push (_stream_readable.js:214:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  name: 'error',
  length: 325,
  severity: 'ERROR',
  code: '23503',
  detail: 'Key (id)=(9452b702-e7e8-4585-adf7-ee84d16306cb) is still referenced from table "comment".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'comment',
  column: undefined,
  dataType: undefined,
  constraint: 'FK_861b419cce1c9ae64295300d6b6',
  file: 'ri_triggers.c',
  line: '3280',
  routine: 'ri_ReportViolation'
}
[Nest] 25768   - 03/28/2020, 4:28:40 PM   {"code":500,"timestamp":"2020-03-28T19:28:40.221Z","path":"/idea/9452b702-e7e8-4585-adf7-ee84d16306cb","method":"DELETE","message":"update or delete on table \"idea\" violates foreign key constraint \"FK_861b419cce1c9ae64295300d6b6\" on table \"comment\""} +12315ms

最佳答案

您在此处设置的cascade 属性用于INSERTUPDATE 操作,如属性文档中所述:

/**
 * Sets cascades options for the given relation.
 * If set to true then it means that related object can be allowed to be inserted or updated in the database.
 * You can separately restrict cascades to insertion or updation using following syntax:
 *
 * cascade: ["insert", "update"] // include or exclude one of them
 */

允许您执行此级联 DELETE 的属性是 onDelete 设置为 CASCADE:

/**
 * Database cascade action on delete.
 */
onDelete?: OnDeleteType;

OnDeleteType 为:

/**
 * ON_DELETE type to be used to specify delete strategy when some relation is being deleted from the database.
 */
export declare type OnDeleteType = "RESTRICT" | "CASCADE" | "SET NULL" | "DEFAULT" | "NO ACTION";

然而,级联删除一直是TypeORM中的一个乏味话题(在GitHub项目上可以看到多个issue)。我假设这在 ManyToMany 上运行良好,但根据您使用的版本,我不能保证结果...

https://stackoverflow.com/questions/60905853/

相关文章:

react-native - 我可以通过 expo 获得在 android 上工作的 webpack

nginx - 为什么我在nginx中无法正常显示CJK字符?

qt - 如何在使用 AUTORCC 的 CMake 项目中使用 qtquick_compiler_

spring-boot - 在 spring cloud 配置更新时重新加载 CamelContex

python - np.array 到 PIL 图像 --> Typerror : Cannot h

python - 在 OR 工具中使用 IntVar

javascript - 为什么ioredis客户端开启keep-alive会超时?

javascript - typescript 装饰器 : Add instance field t

reactjs - 尝试使用 React-Spring 制作动画时出现错误 "TypeError:

scala - 在同一规范文件中测试 akka-http 和 akka actor 时,如何解决冲突