Search code examples
typeormtypegraphql

TypeORM OneToMany Relation not Found


this is my first time posting on StackOverflow so hopefully, I am doing this right.

As I started working with typeORM I noticed that ManyToOne relations are working as intended through my resolvers and display the results, while OneToMany relations throw 'relation can't be found' errors.

Things I've tried so far:

Restructuring entities exactly as in TypeOrm documentation

Query using both QueryBuilder and find methods with relations/leftjoin

Wiping and rebuilding Postgresdb

User.ts File

@Field(() => [UserComment], { nullable: true })
  @OneToMany(() => UserComment, (comment) => comment.user)
  comments: UserComment[];

UserComment.ts File

  @ManyToOne(() => User, (user) => user.comments)
  @Field()
  user: User;

Test query;

return User.findOne(req.session.userId, { relations: ["comments"] });

Output

"message": "Relation \"comments\" was not found; please check if it is correct and really exists in your entity.",

I hope I'm not overlooking something silly. Any suggestions are appreciated.

If it helps, here is to show that many to one side of this relation works as intended:

comment resolver:

  async testComments(): Promise<UserComment[]> {
    return await UserComment.find({ relations: ["user"] });
  }

result:

 "testComments": [
      {
        "body": "This is another test comment.",
        "user": {
          "username": "Admin"
        }
      },
      {
        "body": "Another comment coming up!",
        "user": {
          "username": "Admin"
        }
      },

Solution

  • It was indeed a silly mistake related to previous use of mikro-orm (was using OneToMany auto-import from mikro-orm and not typeorm)