【问题标题】:How to remove prefix from returned columns in typeorm querybuilder如何从 typeorm querybuilder 中的返回列中删除前缀
【发布时间】:2022-03-18 20:17:29
【问题描述】:

我在 typeorm 中设置了以下查询构建器。

const sql = this.attendanceRepository
      .createQueryBuilder("attendance")
      .leftJoin("attendance.child", "child")
      .select("attendance")
      .addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
      .addSelect("child.class")
      .where("attendance.id= :id", { id: id})
      .getRawOne()
      const Result = await sql

该查询构建器产生以下SQL

SELECT `attendance`.`id` AS `attendance_id`, `attendance`.`child_id` AS `attendance_child_id`,
`attendance`.`attendance_cd` AS `attendance_attendance_cd`, `attendance`.`come_home_time` AS `attendance_come_home_time`, `attendance`.`late_time` AS `attendance_late_time`, `attendance`.`memo` AS `attendance_memo`, `attendance`.`created_at` AS `attendance_created_at`,
`attendance`.`updated_at` AS `attendance_updated_at`, `attendance`.`deleted_at` AS `attendance_deleted_at`, `child`.`class` AS `child_class`, 
CONCAT_WS(' ', `child`.`first_name`, `child`.`middle_name`, `child`.`last_name`) AS `childName` 

FROM `attendances` `attendance` 
LEFT JOIN `children` `child` 
ON `child`.`id`=`attendance`.`child_id` 
WHERE ( `attendance`.`id`= ? ) 
AND ( `attendance`.`deleted_at` IS NULL )

它返回了以下数据。

{
    "attendance_id": 2,
    "attendance_child_id": 4,
    "attendance_attendance_cd": 3,
    "attendance_come_home_time": "11:50:00",
    "attendance_late_time": "23:40:00",
    "attendance_memo": "test",
    "attendance_created_at": "2020-10-16T13:33:00.053Z",
    "attendance_updated_at": "2020-10-19T10:34:55.000Z",
    "attendance_deleted_at": null,
    "child_class": "S",
    "childName": "hikaru hikaru"
}

但我想要的结果如下

{
    "id": 2,
    "child_id": 4,
    "attendance_cd": 3,
    "come_home_time": "11:50:00",
    "late_time": "23:40:00",
    "memo": "test",
    "created_at": "2020-10-16T13:33:00.053Z",
    "updated_at": "2020-10-19T10:34:55.000Z",
    "deleted_at": null,
    "child_class": "S",
    "childName": "hikaru hikaru"
}

如何删除前缀 attendance_

如果有人有意见,请告诉我。

谢谢

【问题讨论】:

标签: mysql sql typeorm


【解决方案1】:

请使用 .select("attendance.*")

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

我认为你应该使用getOne 而不是getRawOne,它会自动映射。

const sql = this.attendanceRepository
  .createQueryBuilder("attendance")
  .leftJoin("attendance.child", "child")
  .select("attendance")
  .addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
  .addSelect("child.class")
  .where("attendance.id= :id", { id: id})
  .getOne();

  const Result = await sql;

希望对你有帮助!

【讨论】:

    【解决方案3】:

    目前,2020 年 8 月在 Github 上创建的 TypeOrm Issue 没有官方解决方案https://github.com/typeorm/typeorm/issues/3031

    但您可以映射结果并过滤前缀跟随答案

    Change key name in JavaScript object

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 1970-01-01
      • 2015-10-25
      • 2013-06-10
      • 2023-04-05
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多