繁体   English   中英

Typeorm 给我 QueryFailedError: column reference "id" is ambiguous

[英]Typeorm gives me QueryFailedError: column reference "id" is ambiguous

这是我要发出的查询:

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。

好吧,如您所见,有一个user.id和一个cats.id可用 - 您甚至 select 都是。 但是在WHERE子句中,您没有说明您指的是哪个id 'id =:userId'更改为'user.id =:userId'它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM