【问题标题】:remove relation one-to-many nestjs删除关系一对多的nestjs
【发布时间】:2021-02-04 15:30:56
【问题描述】:

我是 typeorm 和 nestjs 的新手,但是我想知道是否可以删除一个一对多的实体,例如当我使关系看起来像这样时:

id | title | relationId
1    test        2

但是我想删除一个关系回到原始状态,例如:

id | title | relationId
1     test    [null]

然后用新的一对多关系更新它,例如:

id | title | relationId
1     test      4

说实话,我不确定这是否可能

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    您可以在保存调用中将null 作为关系传递,以从记录中清除关系。

    myRepository.save({ id: 1, title: 'test', relation: null })
    

    请注意,您的列必须可以为空,否则 TypeORM(和您的数据库)将强制 relationId 具有值。你可以这样设置:

    @ManyToOne(() => Relation, (myEntity) => myEntity.relations)
    relation: Relation | null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多