无法更新关系n:m

时间:2019-03-04 09:16:18

标签: javascript node.js typeorm

我有两个关系为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[];
}

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

更新逻辑:

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

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]

0 个答案:

没有答案