【问题标题】:typeorm save in joined cloumntypeorm 保存在连接列中
【发布时间】:2020-11-14 23:23:09
【问题描述】:

在我的项目中,我有两个实体,自然主义者(用户)和评论实体 我创建它们之间的关系,如何在评论实体中保存reciever_id (joined 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
}

博物学家(我的用户)实体

@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 保存到我的评论实体

【问题讨论】:

    标签: node.js database postgresql nestjs typeorm


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2021-06-29
      • 2019-04-03
      • 2021-09-08
      • 2021-11-23
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多