如何在 TypeORM 中查询数组并删除多个

How do I query an array and delete multiple in TypeORM

如果 question_id 是 1 或 2,我想删除多个选项。

下面的代码给我这个错误:QueryFailedError: table "option"

缺少 FROM-clause entry

如何解决这个问题?

Service.ts:

this.QuizQuestionOptionRepository.createQueryBuilder('option')
  .delete()
  .from(QuizQuestionOption)
  .where('option.question_id IN (:...id)', { id: [1, 2] })
  .execute();

QuizQuestionOption 实体:

import { QuizQuestion } from './quizQuestion.entity';
import {
  BeforeUpdate,
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class QuizQuestionOption {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column({ nullable: true })
  explanation: string;

  @Column()
  is_correct: boolean;

  @JoinColumn({ name: 'question_id' })
  @ManyToOne(() => QuizQuestion)
  question: QuizQuestion;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  created_at: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  updated_at: Date;

  @BeforeUpdate()
  updateTimestamp() {
    this.updated_at = new Date();
  }
}

我认为您不需要在 createQueryBuilder 中添加名称。也许你应该这样试试

await this.QuizQuestionOptionRepository.createQueryBuilder()
  .delete()
  .from(QuizQuestionOption)
  .where('question_id IN (:...id)', { id: [1, 2] })
  .execute();