【问题标题】:Typeorm / Postgres: for SELECT DISTINCT, ORDER BY expressions must appear in select listTypeorm / Postgres:对于 SELECT DISTINCT,ORDER BY 表达式必须出现在选择列表中
【发布时间】:2023-03-23 09:57:01
【问题描述】:

我有以下实体:用户角色。 我想选择具有分页和过滤器角色的用户。

错误信息:

for SELECT DISTINCT, ORDER BY expressions must appear in the select list
@ObjectType()
@Entity()
export class User {
  *******

  @ManyToMany(() => Role, (role) => role.id, {
    nullable: true,
    cascade: true,
  })
  @JoinTable()
  roles: Role[];

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}

@ObjectType()
@Entity()
export class Role {
  @Field((type) => Int)
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: UserRoles;

  @Column()
  description: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}

主要查询逻辑:

******
    const [results, total] = **await queryBuilder
      .orderBy(`"user_${sortField}"`, sortOrder)
      .take(limit)
      .skip((page - 1) * limit)
      .leftJoinAndSelect('user.roles', 'role')
      .getManyAndCount();**

    return {
      results,
      total,
    };

这会导致错误,因为在生成的 SQL 行 SELECT DISTINCT "distinctAlias"."user_id" as "ids_user_id" 不包含我排序的字段 user_updatedAt。

注意,.take(limit) 方法添加上述行是生成SQL,我找不到编辑选定字段的方法。

生成的 SQL:

SELECT DISTINCT
   \"distinctAlias\".\"user_id\" as \"ids_user_id\" 
FROM
   (
      SELECT
         \"user\".\"id\" AS \"user_id\",
         \"user\".\"firstName\" AS \"user_firstName\",
         \"user\".\"lastName\" AS \"user_lastName\",
         \"user\".\"email\" AS \"user_email\",
         \"user\".\"phone\" AS \"user_phone\",
         \"user\".\"isActivated\" AS \"user_isActivated\",
         \"user\".\"refreshToken\" AS \"user_refreshToken\",
         \"user\".\"createdAt\" AS \"user_createdAt\",
         \"user\".\"updatedAt\" AS \"user_updatedAt\",
         \"role\".\"id\" AS \"role_id\",
         \"role\".\"title\" AS \"role_title\",
         \"role\".\"description\" AS \"role_description\",
         \"role\".\"createdAt\" AS \"role_createdAt\",
         \"role\".\"updatedAt\" AS \"role_updatedAt\" 
      FROM
         \"user\" \"user\" 
         LEFT JOIN
            \"user_roles_role\" \"user_role\" 
            ON \"user_role\".\"userId\" = \"user\".\"id\" 
         LEFT JOIN
            \"role\" \"role\" 
            ON \"role\".\"id\" = \"user_role\".\"roleId\"
   )
   \"distinctAlias\" 
ORDER BY
   \"user_updatedAt\" DESC,
   \"user_id\" ASC LIMIT 10"

我做错了什么? 感谢您的宝贵时间。

【问题讨论】:

  • 错误很明显——只有 SELECT DISTINCT 表达式中的字段可以出现在 ORDER BY 表达式中。 ORDER BY 在SELECT 之后应用,此时没有updatedAt
  • 我理解了这个理论,但使用它仍然无法找到以下代码的正确解决方案: const [results, total] = await queryBuilder .orderBy("user_${sortField}", sortOrder) .leftJoinAndSelect(' user.roles', 'role') .take(limit) .skip((page - 1) * limit) .getManyAndCount();知道如何更改它以解决问题吗?我不明白为什么 Typeorm 只将这些字段 \"distinctAlias\".\"user_id\" as \"ids_user_id\" 添加到 SELECT DISTINCT?

标签: sql postgresql nestjs typeorm


【解决方案1】:

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2012-09-23
  • 1970-01-01
  • 2018-01-16
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
相关资源
最近更新 更多