postgresql - 无法删除 TypeORM 中的 OneToMany 记录

标签 postgresql typescript typeorm

我有以下架构

@Entity()
export class Question extends BaseEntity {
  @PrimaryColumn()
  messageId: string;

  @Column()
  authorId: string;

  @Column()
  question: string;

  @Column("varchar", { array: true })
  possibleAnswers: string[];

  @Column()
  isAnonymous: boolean;

  @OneToMany(() => Answer, (answer) => answer.question, { eager: true })
  answers: Answer[];

  get formattedAnswers() {
    return this.possibleAnswers
      .map((answer, idx) => `${numericEmojis[idx]}: **${answer}**`)
      .join("\n");
  }
}

@Entity()
@Unique("uc_ids", ["userId", "questionMessageId"])
export class Answer extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userId: string;

  @Column()
  answerIndex: number;

  @ManyToOne(() => Question, (question) => question.answers)
  question: Question;

  @Column({ readonly: true })
  // @ts-expect-error
  private readonly questionMessageId: string;
}

每当我尝试删除类似

const question = await Question.findOne(message.id);

await Question.delete(question);

我收到以下错误:

err: query: SELECT "Question"."message_id" AS "Question_message_id", "Question"."author_id" AS "Question_author_id", "Question"."question" AS "Question_question", "Question"."possible_answers" AS "Question_possible_answers", "Question"."is_anonymous" AS "Question_is_anonymous", "Question__answers"."id" AS "Question__answers_id", "Question__answers"."user_id" AS "Question__answers_user_id", "Question__answers"."answer_index" AS "Question__answers_answer_index", "Question__answers"."question_message_id" AS "Question__answers_question_message_id" FROM "question" "Question" LEFT JOIN "answer" "Question__answers" ON "Question__answers"."question_message_id"="Question"."message_id" WHERE "Question"."message_id" IN ($1) -- PARAMETERS: ["729340583583285289"]
err: (node:19515) UnhandledPromiseRejectionWarning: EntityColumnNotFound: No entity column "answers" was found.

最初我试图设置级联删除,这样当我删除问题时,答案也会被删除,我得到了相同的错误,但即使在删除级联删除后我也得到了相同的错误,我该如何解决这个问题?我正在使用带有 SnakeNamingStrategy 的 Postgres 数据库

最佳答案

通过添加 onDelete:"CASCADE"或 onDelete:"SET NULL"对我有用。

I have the following schemas

@Entity()
export class Question extends BaseEntity {
  @PrimaryColumn()
  messageId: string;

  @Column()
  authorId: string;

  @Column()
  question: string;

  @Column("varchar", { array: true })
  possibleAnswers: string[];

  @Column()
  isAnonymous: boolean;

  @OneToMany(() => Answer, (answer) => answer.question, { eager: true })
  answers: Answer[];

  get formattedAnswers() {
    return this.possibleAnswers
      .map((answer, idx) => `${numericEmojis[idx]}: **${answer}**`)
      .join("\n");
  }
}

@Entity()
@Unique("uc_ids", ["userId", "questionMessageId"])
export class Answer extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userId: string;

  @Column()
  answerIndex: number;
  
  // set onDelete as cascade for automatically delete from parent entity
  @ManyToOne(() => Question, (question) => question.answers, { cascade: true, onDelete: "CASCADE" })
  question: Question;

  @Column({ readonly: true })
  // @ts-expect-error
  private readonly questionMessageId: string;
}

关于postgresql - 无法删除 TypeORM 中的 OneToMany 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62741964/

相关文章:

sql - 如何在 PostgreSQL 中对二维 int 数组进行排序?

macos - 通过终端在 Mac 上停止 postgreSQL 服务

sql - 对每个 UserId 实现限制的最快方法

javascript - Angular/Ionic Observer 和 Observable

mysql - 无法使用 leftjoin 从 2 个表中获取所有数据

nestjs - 我可以将实体字段名称映射到 typeorm 中的别名列名称吗?

json - postgresql json_object_keys(var1) 消除 var1 为 null 的记录

angularjs - Angular 2 GET withCredentials(接受来自服务器的cookie)

angular - 在 Angular 2 中替换 Angular 的 $rootScope?

arrays - 为什么我的多对多关系字段未定义?