首页 > 解决方案 > 无法更新关系 n:m

问题描述

我有两个关系为 n:m 的实体

@ObjectType()
@Entity("tags")
export class TagEntity extends BaseEntity {
  @Field(() => ID)
  @PrimaryGeneratedColumn()
  id: number;

  @Field(() => [PostEntity])
  @ManyToMany(() => PostEntity, post => post.tags)
  posts: PostEntity[];
}
@ObjectType()
@Entity("posts")
export class PostEntity extends BaseEntity {
  @Field(() => ID)
  @PrimaryGeneratedColumn()
  id: number;

  @Field(() => [TagEntity])
  @ManyToMany(() => TagEntity, tag => tag.posts)
  @JoinTable()
  tags: TagEntity[];
}

当我尝试使用新标签更新帖子时发出错误:“关系“帖子”的“列”帖子ID“不存在”

更新逻辑:

    await this.postsRepository.update({ id }, postData);

发布数据:

{ id: '1',
  title: 'new post title',
  url: 'new post url',
  tags:
   [ TagEntity {
       id: 2,
       title: 'tag title 2',
       url: 'tag url 2',
       createdAt: 2019-03-04T08:56:50.531Z,
       updatedAt: 2019-03-04T08:56:50.531Z } ] }

生成的sql:

UPDATE "posts" SET "id" = $2, "title" = $3, "url" = $4, "postsId" = $5, "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = $1 -- PARAMETERS: ["1","1","new post title","new post url",null]

标签: javascriptnode.jstypeorm

解决方案


推荐阅读