sqlite TypeORM条件查询的结果不匹配

xoefb8l8  于 2022-11-14  发布在  SQLite
关注(0)|答案(2)|浏览(191)

我在Find语句中设置了WHERE条件,但条件查询WHERE的结果不匹配。
查找查询:

async getRegister() {
    const result = await this.registerModel.findOne({
      where: {
        session: 1,
        user: 2,
      },
      select: ['id', 'created_time'],
    });

    return result;
  }

注册实体:

@Entity('register')
export class Register {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    type: 'text',
    nullable: true,
    unique: true,
  })
  no: string;
  ……
  @ManyToOne(type => User, user => user.registers)
  user: User;

  @OneToOne(type => Session)
  @JoinColumn()
  session: Session;
}

格式化的SQL语句:

query: SELECT DISTINCT "distinctAlias"."Register_id" AS "ids_Register_id" FROM (SELECT "Register"."id" AS "Register_id", "Register"."created_time" AS "Register_created_time" FROM "register" "Register" INNER JOIN "session" "Register__Register_session" ON "Register__Register_session"."id"="Register"."sessionId" AND ("Register__Register_session"."deleted_time" IS NULL)  INNER JOIN "user" "Register__Register_user" ON "Register__Register_user"."id"="Register"."userId" AND ("Register__Register_user"."deleted_time" IS NULL) WHERE "Register"."deleted_time" IS NULL) "distinctAlias" ORDER BY "Register_id" ASC LIMIT 1
query: SELECT "Register"."id" AS "Register_id", "Register"."created_time" AS "Register_created_time" FROM "register" "Register" INNER JOIN "session" "Register__Register_session" ON "Register__Register_session"."id"="Register"."sessionId" AND ("Register__Register_session"."deleted_time" IS NULL)  INNER JOIN "user" "Register__Register_user" ON "Register__Register_user"."id"="Register"."userId" AND ("Register__Register_user"."deleted_time" IS NULL) WHERE ( "Register"."deleted_time" IS NULL ) AND ( "Register"."id" IN (7) )

我找不到参数集

where: {
  session: 1,
  user: 2,
},
x0fgdtte

x0fgdtte1#

当我使用SQL时,我会得到正确的结果。

await this.registerModel.query(
      `select id, created_time from register where userId = ${userId} and sessionId = ${sessionId}`
    );
i7uq4tfw

i7uq4tfw2#

我已经解决了这个问题。首先修改了注册实体

@Entity('register')
export class Register {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    type: 'text',
    nullable: true,
    unique: true,
    comment: '准考证号',
  })
  no: string;

  ……

  @ManyToOne(type => User, user => user.id)
  user: User;

  @ManyToOne(type => Session, session => session.id)
  session: Session;
}

上次修改查找查询的时间:

const result = await this.registerModel.findOne({
      where: {
        user: {
          id: userId,
        },
        session: {
          id: sessionId,
        },
      },
      select: ['id', 'created_time'],
    });

    return result;

相关问题