我正在开发一个创建Ideas api的项目,例如人们可以在其中发布想法的论坛,其他人可以发表评论,支持和反对。
我的数据库面临问题,我不知道如何解决。
错误是:"update or delete on table \"idea\" violates foreign key constraint \"FK_861b419cce1c9ae64295300d6b6\" on table \"comment\"
正如你们看到的那样,我有一个名为“ idea”的表,用于放置想法和评论。
但是当我尝试删除并附加了一些注释的Idea时,会发生此错误。
如果我删除此想法但未添加任何评论,则一切正常。
但是,如果这个想法附有评论,那么这个错误就会出现。
我在网上进行了一些搜索,有人说我要建立我的关系@OneToMany(一个主意,很多评论){级联: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
答案 0 :(得分:0)
您在此处设置的cascade
属性用于INSERT
和UPDATE
操作,如该属性的文档中所述:
/**
* 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项目上看到多个问题)。我认为这与ManyToMany一起使用效果很好,但是根据您使用的版本,我不能保证结果...