Typeorm 给我 QueryFailedError:列引用“id”不明确
Posted
技术标签:
【中文标题】Typeorm 给我 QueryFailedError:列引用“id”不明确【英文标题】:Typeorm gives me QueryFailedError: column reference "id" is ambiguous 【发布时间】:2020-12-08 00:01:03 【问题描述】:这是我要触发的查询:
const updatedUser = await queryRunner.manager
.getRepository(UserEntity)
.createQueryBuilder('user')
.leftJoinAndSelect('user.categories', 'cats')
.where('id = :userId', userId )
.getOne();
这是由 typeorm 生成的查询:
SELECT "user"."id" AS "user_id",
"user"."first_name" AS "user_first_name",
"user"."last_name" AS "user_last_name",
"user"."phone" AS "user_phone",
"user"."password" AS "user_password",
"user"."password_hint" AS "user_password_hint",
"user"."recovery_email" AS "user_recovery_email",
"user"."created" AS "user_created",
"user"."username" AS "user_username",
"user"."profile_pic" AS "user_profile_pic",
"user"."is2fa" AS "user_is2FA",
"user"."refresh_token" AS "user_refresh_token",
"user"."profile_email" AS "user_profile_email",
"user"."subscriptionid" AS "user_subscriptionId",
"cats"."id" AS "cats_id",
"cats"."template_id" AS "cats_template_id",
"cats"."name" AS "cats_name",
"cats"."entry_count" AS "cats_entry_count",
"cats"."is_base_template" AS "cats_is_base_template",
"cats"."can_rename" AS "cats_can_rename",
"cats"."can_delete" AS "cats_can_delete",
"cats"."userid" AS "cats_userId",
"cats"."position" AS "cats_position",
"cats"."icon_path" AS "cats_icon_path",
"cats"."init_name" AS "cats_init_name",
"cats"."isfortasks" AS "cats_isForTasks",
"cats"."is_locked_by" AS "cats_is_locked_by",
"cats"."is_locked" AS "cats_is_locked",
"cats"."password" AS "cats_password",
"cats"."state" AS "cats_state",
"cats"."password_hint" AS "cats_password_hint",
"cats"."parentcategoryid" AS "cats_parentCategoryId"
FROM "user" "user"
LEFT JOIN "category" "cats"
ON "cats"."userid" = "user"."id"
WHERE id = $1
如果我删除 leftJoinAndSelect
它不会产生该错误,但我需要此连接。
查询有什么问题?
元:
我使用的 DBMS 是 PostgreSQL v. 12.3。
【问题讨论】:
【参考方案1】:好吧,正如您所见,有一个 user.id
和一个 cats.id
可用 - 您甚至可以同时选择两者。但是在WHERE
子句中,您没有说出您的意思是哪个id
。将'id = :userId'
更改为'user.id = :userId'
,它应该可以工作。
【讨论】:
以上是关于Typeorm 给我 QueryFailedError:列引用“id”不明确的主要内容,如果未能解决你的问题,请参考以下文章
TypeORM 不会在由 TypeORM 创建的项目中生成迁移