如何在 NestJS 的 typeORM 查询中禁用对我的 postgres 数据库的自动降级?

How to disable automatic downcasing in NestJS' typeORM query to my postgres DB?

我正在使用 TypeORM 通过 TypeORM 实体创建和管理我的数据库。

我有一个非常奇特的关系

@Entity()
export class User extends BaseEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  login: string;

  @Column()
  nickname: string;

  @Column()
  wins: number;

  @Column()
  looses: number;

  @Column()
  current_status: string;

  @ManyToMany(() => User)
  @JoinTable()
  friends: User[];

  @ManyToMany(() => MatchHistory)
  @JoinTable()
  match_histories: MatchHistory[];
}

其中 User 与其自身具有 ManyToMany 关系。因此,典型的工具无法正常工作(我还没有找到使用 TypeORM 的工具访问用户朋友的方法)。

所以我正在做一个很好的旧 SQL 请求:

const res = await this.manager.query("SELECT * FROM user WHERE user.id IN (SELECT F.userId_2 AS Friends FROM user_friends_user F WHERE F.userId_1=?);", [user.id]);

这可以翻译为“获取ID在用户user好友列表中的所有用户。

问题是我注意到我的所有请求似乎都被小写了。这样做时,我面对 column f.userid_2 does not exist.

我不知道是 TypeORM 还是 Postgres 降低了我的请求,我只想停止这样做。非常感谢!

这是 Postgres 默认执行的操作。如果需要使用大写值,则需要将字符串文字传递给 Postgres。在你的情况下,这看起来像

const res = await this.manager.query(
  'SELECT * FROM user WHERE user.id IN (SELECT "F"."userId_2" AS "Friends" FROM user_friends_user "F" WHERE "F"."userId_1"=?);',
  [user.id]
);