1

I am using TypeORM to create and manage my DataBase using TypeORM Entities.

I have a very peculiar relation as such

@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[];
}

Where User has a ManyToMany relationship with itself. Because of this, typical tools do not work correctly (I haven't found a way to access a User's friends with TypeORM's tools).

So I am doing a good old SQL request as such:

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]);

This can be translated as "get all the users who's ID is in the friend list of user user.

The problem is that I have noticed that all my requests seem to be downcased. When doing this I face the column f.userid_2 does not exist.

I do not know if it is TypeORM or Postgres which downcases my requests, all I want is it to stop doing so. Thank you very much !

1 Answer 1

1

This is something Postgres does by default. If you need to use uppercase values, you need to pass a string literal to Postgres. In your case, that would look like

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]
);
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I needed. little precision that, apparently, to the the parameters the correct syntax is @1 (postgres seems to be the only system using this syntax)
Edit: it is actually $1, as in bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.