TypeORM不选择从queryBuilder中提供任何数据



我只想选择表'person'与'contact'内部连接,代码为:

const rep = (await getDatabaseConnection()).getRepository<Contact>('contact')
const build = rep.createQueryBuilder().innerJoin("person", "person").where("person.id= :personId", {personId})
console.log(build.getSql())
console.log(await build.getMany())
console.log(await build.getRawMany())

结果是:

SELECT `Contact`.`id` AS `Contact_id`, `Contact`.`job_title` AS `Contact_job_title`, `Contact`.`email` AS `Contact_email`, `Contact`.`country` AS `Contact_country`, `Contact`.`state` AS `Contact_state`, `Contact`.`personId` AS `Contact_personId` FROM `contact` `Contact` INNER JOIN `person` `person` WHERE `person`.`id`= ?
[]
[]

但是当我在数据库中使用相同的SQL时,比如:

SELECT `Contact`.`id` AS `Contact_id`, `Contact`.`job_title` AS `Contact_job_title`, `Contact`.`email` AS `Contact_email`, `Contact`.`country` AS `Contact_country`, `Contact`.`state` AS `Contact_state`, `Contact`.`personId` AS `Contact_personId` FROM `contact` `Contact` INNER JOIN `person` `person` WHERE `person`.`id`= '75c37eb9'

我可以得到结果:

"id"    "job_title" "email" "country"   "state" "personId"
"e27399a2-8822-4383-8ddb-9ef2a6030299"  "developer" "last@gamil.com"    "Australia" "NSW"   "75c37eb9"

为什么QueryBuilder是空结果?如何为我的案例编写正确的QueryBuilder?

我改为:

const build = rep.createQueryBuilder().innerJoin("person", "person").where({"person.id": personId})

结果仍然一无所获。

同样的事情也发生了:

const build = rep.createQueryBuilder('contact').innerJoinAndSelect("contact.person", "person").where("person.id= :personId", {personId})
relations: ["person"]})
console.log(build.getSql())
console.log(await build.getMany())

结果是:

SELECT `contact`.`id` AS `contact_id`, `contact`.`job_title` AS `contact_job_title`, `contact`.`email` AS `contact_email`, `contact`.`country` AS `contact_country`, `contact`.`state` AS `contact_state`, `contact`.`personId` AS `contact_personId`, `person`.`id` AS `person_id`, `person`.`title` AS `person_title`, `person`.`first_name` AS `person_first_name`, `person`.`last_name` AS `person_last_name`, `person`.`expertise` AS `person_expertise`, `person`.`introduction` AS `person_introduction`, `person`.`COVID_19` AS `person_COVID_19`, `person`.`userId` AS `person_userId`, `person`.`belongOrganizationId` AS `person_belongOrganizationId` FROM `contact` `contact` INNER JOIN `person` `person` ON `person`.`id`=`contact`.`personId` WHERE `person`.`id`= ?
[]

结果为空:

但是如果我使用本机sql:

SELECT `contact`.`id` AS `contact_id`, `contact`.`job_title` AS `contact_job_title`, `contact`.`email` AS `contact_email`, `contact`.`country` AS `contact_country`, `contact`.`state` AS `contact_state`, `contact`.`personId` AS `contact_personId`, `person`.`id` AS `person_id`, `person`.`title` AS `person_title`, `person`.`first_name` AS `person_first_name`, `person`.`last_name` AS `person_last_name`, `person`.`expertise` AS `person_expertise`, `person`.`introduction` AS `person_introduction`, `person`.`COVID_19` AS `person_COVID_19`, `person`.`userId` AS `person_userId`, `person`.`belongOrganizationId` AS `person_belongOrganizationId` FROM `contact` `contact` INNER JOIN `person` `person` ON `person`.`id`=`contact`.`personId` WHERE `person`.`id`='75c37eb9-1d88-4d0c-a927-1f9e3d909aef'

它会给我结果:

{
"table": "UnknownTable",
"rows":
[
{
"contact_id": "e27399a2-8822-4383-8ddb-9ef2a6030299",
"contact_job_title": "developer",
"contact_email": "last@gamil.com",
"contact_country": "Australia",
"contact_state": "NSW",
"contact_personId": "75c37eb9-1d88-4d0c-a927-1f9e3d909aef",
"person_id": "75c37eb9-1d88-4d0c-a927-1f9e3d909aef",
"person_title": "Mr.",
"person_first_name": "sheng",
"person_last_name": "lu",
"person_expertise": "",
"person_introduction": "input introduction",
"person_COVID_19": 0,
"person_userId": "be426167-f471-4092-80dc-7aef67f13bac",
"person_belongOrganizationId": "06078ef6-619f-402f-aaf1-7db1c11de841"
}
]
}

您必须像一样在联接中定义人员关系名称

PD_11这里CCD_2是在实体中定义的关系的名称

最新更新