Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM Query Builder Returning Empty Array When Raw SQL Works

I'm attempting to run the following type of query using createQueryBuilder. I've verified that all of my entities are properly created and joined. However, running the following returns an empty array:

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

But! When I replace getMany() with getSql(), copy the raw SQL and run it, it works! I get the result I expected:

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'

Is there any thing obviously wrong with how I'm trying to build this query?

Thanks in advance for the help!

like image 583
Alex MacArthur Avatar asked Apr 22 '20 13:04

Alex MacArthur


1 Answers

.getMany() returns type Promise of Entity[], and given the nature of your query, I suspect you are not returning an entity at all. Try .getRawMany() or .getRawAndEntities()

See the documentation on QueryBuilder here for more details.

like image 123
mscottnelson Avatar answered Sep 28 '22 04:09

mscottnelson