无法在GetMany Typeorm中选择别名

发布于 2025-02-13 18:56:11 字数 560 浏览 1 评论 0原文

我正在为我的Nestjs应用使用TypeOmm(MySQL)。我想使用QueryBuilder在SELECT上更改列名。但这不起作用。

const order = await this.connection
        .createQueryBuilder(Orders,"order")
        .where("customer_id = :userId", { userId })
        .select(["order.id","order.hashed_id AS hash","order.paid AS payment"])
        .getMany();

同样适用于 leftjoin addSelect

.leftJoin("variety.product","product")
.addSelect([`product.name_${language} AS name`,"product.image_url"])

我们如何将alia设置为typeorm

I'm using TypeORM(mySQL) for my Nestjs App. I want to change the column name on select using querybuilder. but it's not working.

const order = await this.connection
        .createQueryBuilder(Orders,"order")
        .where("customer_id = :userId", { userId })
        .select(["order.id","order.hashed_id AS hash","order.paid AS payment"])
        .getMany();

same applies on leftjoin and addSelect

.leftJoin("variety.product","product")
.addSelect([`product.name_${language} AS name`,"product.image_url"])

How Can we set Alia's in TypeORM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画▽骨i 2025-02-20 18:56:11

我也为此而苦苦挣扎。显然,您必须使用getRawmany()而不是getMany(),因为返回数据不是实体。在此处找到了 https://github.com/typeorm/typeorm/typeorm/typeorm/9114

I was also struggling with this. Apparently you have to use getRawMany() instead of getMany() because the return data is not an entity. Found this out here https://github.com/typeorm/typeorm/issues/9114

酒浓于脸红 2025-02-20 18:56:11

使用TypeOm的QueryBuilder时,您可以为选定的列使用别名。这些别名对于计算的列或结果集中的重命名列很有用。但是,别名本身不包括在最终结果中。

这是使用getMany()使用别名的一个示例:

const orders = await this.connection
    .createQueryBuilder(Orders, "order")
    .where("customer_id = :userId", { userId })
    .select(["order.id"])
    .addSelect(`order.paid`, 'payment')
    .addSelect(`order.hashed_id`, 'hash')
    .getMany();

在此示例中,付款和哈希是order.paid.paid.paid.paid and order.hashed_id的别名。列分别。这些别名在查询中使用,但未包含在结果集中。

如果您需要使用别名并将它们包含在结果集中,则可以使用getRawmany()

const orders = await this.connection
    .createQueryBuilder(Orders, "order")
    .where("customer_id = :userId", { userId })
    .select([
        "order.id",
        "order.hashed_id AS hash",
        "order.paid AS payment"
    ])
    .getRawMany();

在此示例中,使用别名哈希和付款来重命名order> order> order.hashed_id 和order.paid列。这些别名将包括在结果集中。

请记住,当使用getRawmany()的别名使用时,结果集将是一系列具有混叠列名称作为键的对象。

When using TypeORM's QueryBuilder, you can use aliases for selected columns. These aliases can be useful for computed columns or for renaming columns in the result set. However, the aliases themselves are not included in the final result.

Here's an example of using aliases with getMany():

const orders = await this.connection
    .createQueryBuilder(Orders, "order")
    .where("customer_id = :userId", { userId })
    .select(["order.id"])
    .addSelect(`order.paid`, 'payment')
    .addSelect(`order.hashed_id`, 'hash')
    .getMany();

In this example, payment and hash are aliases for the order.paid and order.hashed_id columns, respectively. These aliases are used in the query but are not included in the result set.

If you need to use aliases and include them in the result set, you can use getRawMany():

const orders = await this.connection
    .createQueryBuilder(Orders, "order")
    .where("customer_id = :userId", { userId })
    .select([
        "order.id",
        "order.hashed_id AS hash",
        "order.paid AS payment"
    ])
    .getRawMany();

In this example, the aliases hash and payment are used to rename the order.hashed_id and order.paid columns, respectively. These aliases will be included in the result set.

Remember, when using aliases with getRawMany(), the result set will be an array of objects with the aliased column names as keys.

有木有妳兜一样 2025-02-20 18:56:11

只需删除“ []”。 AddSelect具有两个重叠:

  • addSelect(queryStrs:string [])

  • addSelect(queryStr:string,别来:字符串:string)

      .addSelect(`product.name _ $ {language}作为名称,“ product.image_url”)
     

Just remove "[]". addSelect have two overlap:

  • addSelect(queryStrs: string[])

  • addSelect(queryStr: string, alias: string)

    .addSelect(`product.name_${language} AS name`,"product.image_url")
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文