TypeORM-Querybuilder结果缺少生成的原始查询中存在的联接表数据

时间:2020-10-08 15:33:50

标签: postgresql typeorm

我正在编写一个子查询来取回所有行程。

一个trip有很多trip_items

一个trip_item包含1个trip_item_metadata对象

这是我的查询

    const tripsItems = await getRepository(Trip)
      .createQueryBuilder()
      .select('trips')
      .addSelect('trips_items.uid', 'uid')
      .addSelect('trips_items.trips_items_metadata_color', 'color')
      .from(Trip, 'trips')
      .leftJoinAndSelect(
        subQuery => {
          return subQuery
            .select('trips_items.*')
            .addSelect('trips_items_metadata.color')
            .from(TripItem, 'trips_items')
            .innerJoin('trips_items.tripItemMetadata', 'trips_items_metadata')
        },
        'trips_items',
        'trips_items.trip_id = trips.trip_id',
      )
      .getMany()

这是TypeORM的输出

tripsItems [
  {
    "trip_id": "60d163d1-4e55-4863-b09e-7fdec3b57c64",
    "uid": "b13f60b5-a4ea-4b97-8a44-76bb7144e15d",
    "destination": "Budapest, Hungary",
    "start_date": "2020-10-08",
    "end_date": "2020-10-15",
    "created_by": "68f70888-ee0e-4bbb-b09d-4249c12ac8da",
    "updated_by": null,
    "deleted_by": null
  }
]

这是运行TypeORM生成的原始查询的输出

SELECT
   "trips"."trip_id" AS "trips_trip_id",
   "trips"."uid" AS "trips_uid",
   "trips"."destination" AS "trips_destination",
   "trips"."start_date" AS "trips_start_date",
   "trips"."end_date" AS "trips_end_date",
   "trips"."created_by" AS "trips_created_by",
   "trips"."updated_by" AS "trips_updated_by",
   "trips"."deleted_by" AS "trips_deleted_by",
   "trips"."user_member_id" AS "trips_user_member_id",
   "trips_items".*,
   trips_items.uid AS "uid",
   trips_items.trips_items_metadata_color AS "color" 
FROM
   "trips" "Trip",
   "trips" "trips" 
   LEFT JOIN
      (
         SELECT
            "trips_items_metadata"."color" AS "trips_items_metadata_color",
            trips_items.* 
         FROM
            "trips_items" "trips_items" 
            INNER JOIN
               "trips_items_metadata" "trips_items_metadata" 
               ON "trips_items_metadata"."trip_item_metadata_id" = "trips_items"."trip_item_metadata_id"
      )
      "trips_items" 
      ON trips_items.trip_id = "trips"."trip_id"

因此,在postgres中运行上述命令将返回正确的结果-常规类型由于某些原因而忽略的color列丢失了(我将其作为两次独立的列两次进行了测试)

            trips_trip_id             |              trips_uid               | trips_destination | trips_start_date | trips_end_date |           trips_created_by           | trips_updated_by | trips_deleted_by |         trips_user_member_id         | trips_items_metadata_color |             trip_item_id             |                 uid                  |   destination   | start_date |  end_date  |       start_time        |        end_time         |              created_by              | updated_by | deleted_by | note | itinerary |               trip_id                |        trip_item_metadata_id         |                 uid                  |  color  
--------------------------------------+--------------------------------------+-------------------+------------------+----------------+--------------------------------------+------------------+------------------+--------------------------------------+----------------------------+--------------------------------------+--------------------------------------+-----------------+------------+------------+-------------------------+-------------------------+--------------------------------------+------------+------------+------+-----------+--------------------------------------+--------------------------------------+--------------------------------------+---------
 60d163d1-4e55-4863-b09e-7fdec3b57c64 | b13f60b5-a4ea-4b97-8a44-76bb7144e15d | Budapest, Hungary | 2020-10-08       | 2020-10-15     | 68f70888-ee0e-4bbb-b09d-4249c12ac8da |                  |                  | c06bafd7-49ad-4d46-93f4-cdeb34904602 | #7fa5e8                    | b21f8f22-2bbb-43c4-83d2-c8ed5745cfcc | a04b532b-56b5-45ad-95d4-82c0f8aea6b6 | Vienna, Austria | 2020-10-08 | 2020-10-08 | 2020-10-07 21:55:21.147 | 2020-10-07 21:55:21.147 | 68f70888-ee0e-4bbb-b09d-4249c12ac8da |            |            |      | f         | 60d163d1-4e55-4863-b09e-7fdec3b57c64 | 835dff5b-3563-4371-a491-169da388d5cc | a04b532b-56b5-45ad-95d4-82c0f8aea6b6 | #7fa5e8

为什么TypeORM忽略原始输出中包含的列?

0 个答案:

没有答案