提问者:小点点

typeorm保存在已连接的云中n


在我的项目中,我有两个实体,自然主义者(用户)和注释实体,我创建了它们之间的关系,如何在注释实体中保存reciever_id(加入cloumn

这是我的注释实体

@Entity()
export class Comments extends BaseEntity {

@PrimaryGeneratedColumn()
id: string

@ManyToOne(
type => Naturalist,
naturalist => naturalist.comments
)
@JoinColumn({
name: 'naturalist_id',
})
naturalist: Naturalist


@Column({ nullable: true, default: '' })
text?: string

@Column({ nullable: true, default: '' })
sender_id?: string
}

Naturist(我的用户)实体

@Entity()
export class Naturalist extends BaseEntity {

@PrimaryGeneratedColumn()
id: number

@Column()
user_id: string


@OneToMany(
type => Comments,
comment => comment.naturalist
)
comments: Comments[]

}

DTO包含

{
receiver_id: '2cc2f359-821c-4940-99ae-7386576d861b',
text: 'string',
id: 'e0464049-d1d9-474a-af5b-815805aa1c4b'
}

我想将receiver_id保存到我的注释实体


共1个答案

匿名用户

如果我正确理解您的需要,
我认为您需要一个insert方法来执行此操作:

await entityManager.insert(Comments, { naturalist: { id: '2cc2f359-821c-4940-99ae-7386576d861b' } as Naturalist });