typeorm: Ordering not working/ relations not loading in query strategy within transactions

Issue description

When creating a query builder and call setFindOption containing order and relation, if the load strategy is set to query, the resulted query seams to be wrong. The check to find the join is operated on the joins collection from the SelectQueryBuilder while the relations are in the relationMetadatas. Therefore a new join is created with select: false

Expected Behavior

I should be able to order by a relation column while using a relation load strategy query. It works only with join relation strategy

Actual Behavior

The order seams not to be available, even if I try by any means to select the relation columns it does not work. If I am doing something wrong, please let me know and I ll test.

Here is the output query

SELECT DISTINCT "distinctAlias"."Product_id" AS "ids_Product_id", "distinctAlias"."Product__Product_variants_title"
FROM (SELECT "Product"."id"             AS "Product_id",
             "Product"."created_at"     AS "Product_created_at",
             "Product"."updated_at"     AS "Product_updated_at",
             "Product"."deleted_at"     AS "Product_deleted_at",
             "Product"."title"          AS "Product_title",
             "Product"."subtitle"       AS "Product_subtitle",
             "Product"."description"    AS "Product_description",
             "Product"."handle"         AS "Product_handle",
             "Product"."is_giftcard"    AS "Product_is_giftcard",
             "Product"."status"         AS "Product_status",
             "Product"."thumbnail"      AS "Product_thumbnail",
             "Product"."profile_id"     AS "Product_profile_id",
             "Product"."weight"         AS "Product_weight",
             "Product"."length"         AS "Product_length",
             "Product"."height"         AS "Product_height",
             "Product"."width"          AS "Product_width",
             "Product"."hs_code"        AS "Product_hs_code",
             "Product"."origin_country" AS "Product_origin_country",
             "Product"."mid_code"       AS "Product_mid_code",
             "Product"."material"       AS "Product_material",
             "Product"."collection_id"  AS "Product_collection_id",
             "Product"."type_id"        AS "Product_type_id",
             "Product"."discountable"   AS "Product_discountable",
             "Product"."external_id"    AS "Product_external_id",
             "Product"."metadata"       AS "Product_metadata",
      FROM "product" "Product"
               LEFT JOIN "product_variant" "Product__Product_variants"
                         ON "Product__Product_variants"."product_id" = "Product"."id" AND
                            ("Product__Product_variants"."deleted_at" IS NULL)
      WHERE (("Product"."status" IN ($1)))
        AND ("Product"."deleted_at" IS NULL)) "distinctAlias"
ORDER BY "distinctAlias"."Product__Product_variants_title" DESC, "Product_id" ASC
LIMIT 100

Therefore the following error is thrown QueryFailedError: column distinctAlias.Product__Product_variants_title does not exist

Steps to reproduce

const productAlias = "product"
const queryBuilder = this.createQueryBuilder(productAlias)
queryBuilder.expressionMap.relationLoadStrategy = "query"

const options_ = { ...options }

// ..other constraint and things happen here but does not concern this issue since the conditions are not meet

const res = await queryBuilder.setFindOptions(options_).getManyAndCount()

and here is the options_ value

{
  "where": {
    "status": {
      "_type": "in",
      "_value": [
        "published"
      ],
      "_useParameter": true,
      "_multipleParameters": true
    }
  },
  "skip": 0,
  "take": 100,
  "relations": {
    "variants": {
      "prices": true,
      "options": true
    },
    "options": {
      "values": true
    },
    "images": true,
    "tags": true,
    "collection": true,
    "type": true
  },
  "order": {
    "variants": {
      "title": "DESC"
    }
  }
}

My Environment

Dependency Version
Operating System
Node.js version 17.6
Typescript version 4.7
TypeORM version 0.3.11

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don’t know how to start. I would need guidance.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 22 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@pleerock any update on these ones? It is a pretty important issue that we have here