user13103906
user13103906

Reputation:

Cannot delete a OneToMany record in TypeORM

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;

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

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

Whenever I try to delete like

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

await Question.delete(question);

I get the following error:

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.

Originally I was trying to setup a cascade delete so that when I remove a question, the answers are removed as well, I got the same error but even after removing the cascade delete I get the same one, how can I fix this? I am using a Postgres database with the SnakeNamingStrategy

Upvotes: 2

Views: 5837

Answers (1)

Ade Suhada
Ade Suhada

Reputation: 21

Works for me by adding onDelete:"CASCADE" or 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;
}

Upvotes: 2

Related Questions