作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 NestJS 与 TypeORM 一起使用,并且我正在尝试使用消息保存用户对话。我将对话实体上的消息字段设置为 级联:真 .但是当我尝试这段代码时:
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' }
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;
}
最佳答案
外键约束在您的数据库表上。 RDBMS 术语中的级联意味着它会在每个关系中传播。我相信您创建的 adminID 不是用户表中的用户。您需要先查询该用户,然后将其用于消息推送。这应该可以解决您的问题,伙计。
关于nestjs - TypeORM 问题保存实体级联 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58554432/
我是一名优秀的程序员,十分优秀!