我正在尝试使用createQueryBuilder
运行以下类型的查询。我已验证所有实体均已正确创建并加入。但是,运行以下命令将返回一个空数组:
let apiKey = await getConnection()
.createQueryBuilder()
.from(ApiKey, "apiKey")
.innerJoinAndSelect("apiKey.site", "site")
.where("site.domain = :domain", { domain: "mysitename.com" })
.andWhere("apiKey.key = :key", { key })
.getMany()
但是!当我将getMany()
替换为getSql()
时,复制原始SQL并运行它,就可以了!我得到了我期望的结果:
SELECT "site"."id" AS "site_id", "site"."domain" AS "site_domain", "site"."name" AS "site_name", "site"."createdAt" AS "site_createdAt", "site"."apiKeyId" AS "site_apiKeyId", "site"."userId" AS "site_userId" FROM "api_key" "apiKey" INNER JOIN "site" "site" ON "site"."id"="apiKey"."siteId" WHERE "site"."domain" = 'mysitename.com'
我试图建立此查询的方式有什么明显的问题吗?
提前感谢您的帮助!
答案 0 :(得分:3)
.getMany()返回Promise of Entity []的类型,鉴于查询的性质,我怀疑您根本没有返回任何实体。尝试.getRawMany()或.getRawAndEntities()