【问题标题】:TypeORM get data where id in jsonb arrayTypeORM 获取 jsonb 数组中 id 的数据
【发布时间】:2021-10-31 20:42:22
【问题描述】:

我有一个实体:

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

  @Column('jsonb')
  workoutExercises: Exercise[];
}

运动实体:

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

  @Column()
  name: string;
}

数据示例:

[
  {
    "workoutName": "Monday",
    "workoutExercises": [
      {
        "id": 1,
        "sets": 4,
        "weight": 100,
        "repetitions": 10
      }
    ],
  },
  {
    "workoutName": "Sunday",
    "workoutExercises": [
      {
        "id": 2,
        "sets": 4,
        "weight": 100,
        "repetitions": 10
      }
    ],
  }
]

我想找到 id 为 1 的实体。

在 abdul Samad 回答之后,我尝试了这个:

this.createQueryBuilder('training')
      .where(`:exerciseId IN ("training"."workoutExercises"->>id)`, {
        exerciseId,
      })
      .getMany();

这会返回一个空数组。应该返回一些东西。

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:
    this.createQueryBuilder('training')
          .where('workoutExercises->>id IN (:exerciseId)', { exerciseId })
          .getMany();
    

    希望这会奏效:)。

    【讨论】:

    • 这会返回一个新错误,提示“列“workoutexercises”不存在”。此外,不应该是 .where(':exerciseId IN (workoutExercises->>id)', { exerciseId }) 之类的东西吗? exerciseId 是一个 id,锻炼锻炼是锻炼的数组。
    猜你喜欢
    • 2020-04-13
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多