TypeORM 问题保存实体级联 true [英] TypeORM problem saving entity with cascade true

查看:116
本文介绍了TypeORM 问题保存实体级联 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 NestJS 与 TypeORM 一起使用,并且我正在尝试使用消息保存用户对话.我将对话实体上的消息字段设置为 cascade: true.但是当我尝试这段代码时:

I m using NestJS with TypeORM and I m trying to save a user conversation with messages. I set the messages field on the conversation entity to cascade: true. But When I try this code:

const user3: User = { login: 'admin', createdBy: 'system', lastModifiedBy: 'system' };
const user4: User = { login: 'user', createdBy: 'system', lastModifiedBy: 'system' };
const message1: Message = { content: 'Hello How are you? ', createdBy: user3.login, lastModifiedBy: user3.login };
const conversation1: Conversation = { sender: user3, reciever: user4, messages: [message1] };
getConnection().getRepository(Conversation).save(conversation1);

它创建了这个查询:

INSERT INTO "message"("id", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "content", "conversationSenderId", "conversationRecieverId", "conversationDate") VALUES (?, ?, datetime('now'), ?, datetime('now'), ?, ?, ?, ?)
-- PARAMETERS: ["67348880-6897-47cb-92ef-5f66ffb2e88c","admin","admin","Hello How are you? ","b2420445-c8ee-4080-86b3-98ab12ef576b","09f3672d-9b2f-4f68-b47e-a7ca5d806ec6","2019-10-24 14:41:40.000"]

出现此错误:

{ [Error: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed] errno: 19, code: 'SQLITE_CONSTRAINT' }

我可以看到消息表的外键有问题,但我无法修复它,因为它是处理创建的 TypeORM 库,而不是我.我在互联网上搜索(文档、github、stackoverflow)但没有成功.我认为这是 TypeORM 库中的错误,但我是其中的新手,所以我更喜欢问我是否做错了什么

I can see it's a problem with the foreign key of messages table but I can't fix it because it's TypeORM library which handle the creation, not me. I searched on internet (documentation, github, stackoverflow) without success. I think it's bug in TypeORM library but I m a novice in it so I prefer asking if I did something wrong

这是我的实体:

export abstract class BaseEntity {
  @ObjectIdColumn()
  @PrimaryGeneratedColumn('uuid')
  id?: string;

  @Column()
  createdBy?: string;

  @CreateDateColumn()
  createdDate?: Date;

  @Column()
  lastModifiedBy?: string;

  @UpdateDateColumn()
  lastModifiedDate?: Date;
}

@Entity('user')
export class User extends BaseEntity {
  @Column()
  login?: string;

  @OneToMany(type => Conversation, conversation => conversation.sender)
  sentConversations?: Conversation[];

  @OneToMany(type => Conversation, conversation => conversation.reciever)
  recievedConversations?: Conversation[];
}

@Entity()
export class Conversation {
  @PrimaryColumn()
  senderId?: string;

  @PrimaryColumn()
  recieverId?: string;

  @CreateDateColumn({ primary: true })
  date?: Date;

  @OneToMany(type => Message, message => message.conversation, { cascade: true })
  messages: Message[];

  @ManyToOne(type => User, user => user.sentConversations)
  @JoinColumn({ name: 'senderId' })
  sender?: User;

  @ManyToOne(type => User, user => user.recievedConversations)
  @JoinColumn({ name: 'recieverId' })
  reciever?: User;
}

@Entity('message')
export class Message extends BaseEntity {
  @Column({ length: 10000 })
  content: string;

  @ManyToOne(type => Conversation, conversation => conversation.messages)
  conversation?: Conversation;
}

在此存储库中,您将能够重现错误:https://github.com/youtix/nest-typerorm-test

In this repository, you will be able to reproduce the bug: https://github.com/youtix/nest-typerorm-test

推荐答案

我检查了你的 git 项目,如果你改变 orm.config.js 中的 synchronize: false 一切应该可以正常工作

I checked your git project, if you change synchronize: false in orm.config.js everything should work fine

这篇关于TypeORM 问题保存实体级联 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆