TypeORM 不 select 提供来自 queryBuilder 的任何数据

TypeORM do not select give any data from queryBuilder

我只想 select table '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"
        }
    ]
}

你必须在 join like 中定义人际关系名称

const result = getRepository(Contact)
    .createQueryBuilder('contact')
    .innerJoinAndSelect("contact.person", "person")
    .where("person.id= :personId", {personId})

这里contact.person是实体中定义的关系名称