Home > other >  TypeORM The results of the conditional query were not match
TypeORM The results of the conditional query were not match

Time:10-06

I set the where condition in the find statement, but the results of the conditional query where not match.

The find query:

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

    return result;
  }

The register entity:

@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;
}

The formatted SQL statement:

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) )

I could not find the parameter set

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

CodePudding user response:

When I use sql, I get the right results.

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

CodePudding user response:

I have fixed this issuse. First modified the registered entity

@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;
}

Last modified the find query:

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

    return result;
  • Related