TypeORM select 所有行但限制为 25
TypeORM select all rows but limit 25
CandidateEntity
@Entity({ name: 'users' })
export class CandidateEntity {
@PrimaryGeneratedColumn()
public id: number;
@OneToOne(() => CandidateEmployeeInfoEntity, employeeInfo => employeeInfo.candidate)
public employeeInfo: CandidateEmployeeInfoEntity;
}
EmployeeInfoEntity
@Entity({ name: 'candidates_employee_infos' })
export class CandidateEmployeeInfoEntity {
@PrimaryGeneratedColumn()
public id: number;
@Column({ type: 'bool', nullable: false })
public relocation: boolean;
@Column({ type: 'text', nullable: true })
public softSkills: string;
@OneToOne(() => CandidateEntity, candidate => candidate.employeeInfo)
public candidate: CandidateEntity;
@Column({ type: 'integer' })
public candidateId: number;
}
我创建查询 select 数据库中 104 行中的前 25 行
const {
perPage = 25,
page = 1,
} = params;
const skip = (perPage * page) - perPage;
let candidatesQuery = this.candidateRepository.createQueryBuilder('candidates');
candidatesQuery = candidatesQuery.leftJoinAndSelect(`candidates.employeeInfo`, 'employeeInfo'); // problem in this relation
candidatesQuery = candidatesQuery.skip(skip);
candidatesQuery = candidatesQuery.take(perPage);
const { entities, raw } = await candidatesQuery.getRawAndEntities();
const count = await candidatesQuery.getCount();
console.log(entities.length) // 104 rows
console.log(raw.length) // 104 rows
console.log(count) // 104 rows
当 typeorm return 不正确结果时输出 sql 查询
第一次查询
SELECT DISTINCT "distinctAlias"."candidates_id" as "ids_candidates_id" FROM (SELECT "candidates"."id" AS "candidates_id", "candidates"."uuid" AS "candidates_uuid", "candidates"."role" AS "candidates_role", "candidates"."first_name" AS "candidates_first_name", "candidates"."last_name" AS "candidates_last_name", "candidates"."email" AS "candidates_email", "candidates"."phone" AS "candidates_phone", "candidates"."phone_prefix" AS "candidates_phone_prefix", "candidates"."country_id" AS "candidates_country_id", "candidates"."city_id" AS "candidates_city_id", "candidates"."avatar" AS "candidates_avatar", "candidates"."confirmed_at" AS "candidates_confirmed_at", "candidates"."is_generated" AS "candidates_is_generated", "candidates"."created_at" AS "candidates_created_at", "candidates"."birthday" AS "candidates_birthday", "candidates"."type" AS "candidates_type", "employeeInfo"."id" AS "employeeInfo_id", "employeeInfo"."hourly_rate_from" AS "employeeInfo_hourly_rate_from", "employeeInfo"."hourly_rate_to" AS "employeeInfo_hourly_rate_to", "employeeInfo"."hourly_rate_currency" AS "employeeInfo_hourly_rate_currency", "employeeInfo"."salary_rate_from" AS "employeeInfo_salary_rate_from", "employeeInfo"."salary_rate_to" AS "employeeInfo_salary_rate_to", "employeeInfo"."salary_rate_currency" AS "employeeInfo_salary_rate_currency", "employeeInfo"."relocation" AS "employeeInfo_relocation", "employeeInfo"."soft_skills" AS "employeeInfo_soft_skills", "employeeInfo"."candidate_id" AS "employeeInfo_candidate_id" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()) "distinctAlias" ORDER BY "candidates_id" ASC LIMIT 25
第二次查询
SELECT "candidates"."id" AS "candidates_id", "candidates"."uuid" AS "candidates_uuid", "candidates"."role" AS "candidates_role", "candidates"."first_name" AS "candidates_first_name", "candidates"."last_name" AS "candidates_last_name", "candidates"."email" AS "candidates_email", "candidates"."phone" AS "candidates_phone", "candidates"."phone_prefix" AS "candidates_phone_prefix", "candidates"."country_id" AS "candidates_country_id", "candidates"."city_id" AS "candidates_city_id", "candidates"."avatar" AS "candidates_avatar", "candidates"."confirmed_at" AS "candidates_confirmed_at", "candidates"."is_generated" AS "candidates_is_generated", "candidates"."created_at" AS "candidates_created_at", "candidates"."birthday" AS "candidates_birthday", "candidates"."type" AS "candidates_type", "employeeInfo"."id" AS "employeeInfo_id", "employeeInfo"."hourly_rate_from" AS "employeeInfo_hourly_rate_from", "employeeInfo"."hourly_rate_to" AS "employeeInfo_hourly_rate_to", "employeeInfo"."hourly_rate_currency" AS "employeeInfo_hourly_rate_currency", "employeeInfo"."salary_rate_from" AS "employeeInfo_salary_rate_from", "employeeInfo"."salary_rate_to" AS "employeeInfo_salary_rate_to", "employeeInfo"."salary_rate_currency" AS "employeeInfo_salary_rate_currency", "employeeInfo"."relocation" AS "employeeInfo_relocation", "employeeInfo"."soft_skills" AS "employeeInfo_soft_skills", "employeeInfo"."candidate_id" AS "employeeInfo_candidate_id" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()
第三次查询
SELECT COUNT(DISTINCT("candidates"."id")) as "cnt" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()
当我删除负载关系时employeeInfo
。
这条线
candidatesQuery = candidatesQuery.leftJoinAndSelect('candidates.employeeInfo', 'employeeInfo');
TypeORM return 25 行
console.log(entities.length) // 25 rows
console.log(raw.length) // 25 rows
console.log(count) // 104 rows
为什么?以及如何解决这个问题?
当您使用 Leftjoin 时,typeorm 不会将 LIMIT 添加到其查询中。
来自 TypeORM 的创建者:
take and skip functionality does not work with raw data since its internal ORM functionality
https://github.com/typeorm/typeorm/issues/1768
所以要修复它有两种选择:
如果你想让typeORM为你做,你应该使用getMany,而不是raw
如果您想要原始结果,您可以使用 .offset
和 .limit
而不是 .skip
、.take
。由于 .offset
和 .limit
将更改 SQL 查询。
注意:在实现分页时按某些东西排序是个好主意
CandidateEntity
@Entity({ name: 'users' })
export class CandidateEntity {
@PrimaryGeneratedColumn()
public id: number;
@OneToOne(() => CandidateEmployeeInfoEntity, employeeInfo => employeeInfo.candidate)
public employeeInfo: CandidateEmployeeInfoEntity;
}
EmployeeInfoEntity
@Entity({ name: 'candidates_employee_infos' })
export class CandidateEmployeeInfoEntity {
@PrimaryGeneratedColumn()
public id: number;
@Column({ type: 'bool', nullable: false })
public relocation: boolean;
@Column({ type: 'text', nullable: true })
public softSkills: string;
@OneToOne(() => CandidateEntity, candidate => candidate.employeeInfo)
public candidate: CandidateEntity;
@Column({ type: 'integer' })
public candidateId: number;
}
我创建查询 select 数据库中 104 行中的前 25 行
const {
perPage = 25,
page = 1,
} = params;
const skip = (perPage * page) - perPage;
let candidatesQuery = this.candidateRepository.createQueryBuilder('candidates');
candidatesQuery = candidatesQuery.leftJoinAndSelect(`candidates.employeeInfo`, 'employeeInfo'); // problem in this relation
candidatesQuery = candidatesQuery.skip(skip);
candidatesQuery = candidatesQuery.take(perPage);
const { entities, raw } = await candidatesQuery.getRawAndEntities();
const count = await candidatesQuery.getCount();
console.log(entities.length) // 104 rows
console.log(raw.length) // 104 rows
console.log(count) // 104 rows
当 typeorm return 不正确结果时输出 sql 查询
第一次查询
SELECT DISTINCT "distinctAlias"."candidates_id" as "ids_candidates_id" FROM (SELECT "candidates"."id" AS "candidates_id", "candidates"."uuid" AS "candidates_uuid", "candidates"."role" AS "candidates_role", "candidates"."first_name" AS "candidates_first_name", "candidates"."last_name" AS "candidates_last_name", "candidates"."email" AS "candidates_email", "candidates"."phone" AS "candidates_phone", "candidates"."phone_prefix" AS "candidates_phone_prefix", "candidates"."country_id" AS "candidates_country_id", "candidates"."city_id" AS "candidates_city_id", "candidates"."avatar" AS "candidates_avatar", "candidates"."confirmed_at" AS "candidates_confirmed_at", "candidates"."is_generated" AS "candidates_is_generated", "candidates"."created_at" AS "candidates_created_at", "candidates"."birthday" AS "candidates_birthday", "candidates"."type" AS "candidates_type", "employeeInfo"."id" AS "employeeInfo_id", "employeeInfo"."hourly_rate_from" AS "employeeInfo_hourly_rate_from", "employeeInfo"."hourly_rate_to" AS "employeeInfo_hourly_rate_to", "employeeInfo"."hourly_rate_currency" AS "employeeInfo_hourly_rate_currency", "employeeInfo"."salary_rate_from" AS "employeeInfo_salary_rate_from", "employeeInfo"."salary_rate_to" AS "employeeInfo_salary_rate_to", "employeeInfo"."salary_rate_currency" AS "employeeInfo_salary_rate_currency", "employeeInfo"."relocation" AS "employeeInfo_relocation", "employeeInfo"."soft_skills" AS "employeeInfo_soft_skills", "employeeInfo"."candidate_id" AS "employeeInfo_candidate_id" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()) "distinctAlias" ORDER BY "candidates_id" ASC LIMIT 25
第二次查询
SELECT "candidates"."id" AS "candidates_id", "candidates"."uuid" AS "candidates_uuid", "candidates"."role" AS "candidates_role", "candidates"."first_name" AS "candidates_first_name", "candidates"."last_name" AS "candidates_last_name", "candidates"."email" AS "candidates_email", "candidates"."phone" AS "candidates_phone", "candidates"."phone_prefix" AS "candidates_phone_prefix", "candidates"."country_id" AS "candidates_country_id", "candidates"."city_id" AS "candidates_city_id", "candidates"."avatar" AS "candidates_avatar", "candidates"."confirmed_at" AS "candidates_confirmed_at", "candidates"."is_generated" AS "candidates_is_generated", "candidates"."created_at" AS "candidates_created_at", "candidates"."birthday" AS "candidates_birthday", "candidates"."type" AS "candidates_type", "employeeInfo"."id" AS "employeeInfo_id", "employeeInfo"."hourly_rate_from" AS "employeeInfo_hourly_rate_from", "employeeInfo"."hourly_rate_to" AS "employeeInfo_hourly_rate_to", "employeeInfo"."hourly_rate_currency" AS "employeeInfo_hourly_rate_currency", "employeeInfo"."salary_rate_from" AS "employeeInfo_salary_rate_from", "employeeInfo"."salary_rate_to" AS "employeeInfo_salary_rate_to", "employeeInfo"."salary_rate_currency" AS "employeeInfo_salary_rate_currency", "employeeInfo"."relocation" AS "employeeInfo_relocation", "employeeInfo"."soft_skills" AS "employeeInfo_soft_skills", "employeeInfo"."candidate_id" AS "employeeInfo_candidate_id" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()
第三次查询
SELECT COUNT(DISTINCT("candidates"."id")) as "cnt" FROM "users" "candidates" LEFT JOIN "candidates_employee_infos" "employeeInfo" ON "employeeInfo"."candidate_id"="candidates"."id" WHERE "candidates"."type" IN ()
当我删除负载关系时employeeInfo
。
这条线
candidatesQuery = candidatesQuery.leftJoinAndSelect('candidates.employeeInfo', 'employeeInfo');
TypeORM return 25 行
console.log(entities.length) // 25 rows
console.log(raw.length) // 25 rows
console.log(count) // 104 rows
为什么?以及如何解决这个问题?
当您使用 Leftjoin 时,typeorm 不会将 LIMIT 添加到其查询中。
来自 TypeORM 的创建者:
take and skip functionality does not work with raw data since its internal ORM functionality https://github.com/typeorm/typeorm/issues/1768
所以要修复它有两种选择:
如果你想让typeORM为你做,你应该使用getMany,而不是raw
如果您想要原始结果,您可以使用
.offset
和.limit
而不是.skip
、.take
。由于.offset
和.limit
将更改 SQL 查询。
注意:在实现分页时按某些东西排序是个好主意