无法在GetMany Typeorm中选择别名
我正在为我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也为此而苦苦挣扎。显然,您必须使用
getRawmany()
而不是getMany()
,因为返回数据不是实体。在此处找到了 https://github.com/typeorm/typeorm/typeorm/typeorm/9114I was also struggling with this. Apparently you have to use
getRawMany()
instead ofgetMany()
because the return data is not an entity. Found this out here https://github.com/typeorm/typeorm/issues/9114使用TypeOm的QueryBuilder时,您可以为选定的列使用别名。这些别名对于计算的列或结果集中的重命名列很有用。但是,别名本身不包括在最终结果中。
这是使用
getMany()
使用别名的一个示例:在此示例中,付款和哈希是
order.paid.paid.paid.paid
andorder.hashed_id
的别名。列分别。这些别名在查询中使用,但未包含在结果集中。如果您需要使用别名并将它们包含在结果集中,则可以使用
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()
:In this example, payment and hash are aliases for the
order.paid
andorder.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()
:In this example, the aliases hash and payment are used to rename the
order.hashed_id
andorder.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.只需删除“ []”。 AddSelect具有两个重叠:
addSelect(queryStrs:string [])
addSelect(queryStr:string,别来:字符串:string)
Just remove "[]". addSelect have two overlap:
addSelect(queryStrs: string[])
addSelect(queryStr: string, alias: string)