Typeorm many to many relation joinColumns

Viewed 5416

I'm new to typeorm and I'm trying to make this orm work as a charm. I have no biggie with simple entities : Photos and albums. I have a joining table to know which picture is inside which album.

So I have the following :

import {
  Entity, PrimaryGeneratedColumn, Column, OneToMany, JoinTable,
} from 'typeorm';
import { PicturesModel } from '../pictures/pictures.model';

@Entity({
  name: 'T_ALBUM_AL',
  synchronize: true,
})
export class AlbumModel {
  @PrimaryGeneratedColumn({
    name: 'AL_id',
  })
  id: number;

  @Column({
    name: 'AL_name',
  })
  name: string;

  @Column({
    name: 'AL_created_at',
  })
  created_at: Date;

  @Column({
    name: 'AL_updated_at',
  })
  updated_at: Date;

  @JoinTable({
    name: 'TJ_PICTURE_ALBUM_PA',
    joinColumn: {
      name: 'PI_id',
      referencedColumnName: 'PI_id',
    },
  })
  @OneToMany(() => PicturesModel, (pictures: PicturesModel) => pictures.id)
  pictures: PicturesModel[]
}


import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity({
  name: 'T_PICTURE_PI',
  synchronize: true,
})
export class PicturesModel {
  @PrimaryGeneratedColumn({
    name: 'PI_id',
  })
  id: number;

  @Column({
    name: 'PI_name',
  })
  name: string;

  @Column({
    name: 'PI_path',
  })
  path: string;

  @Column({
    name: 'PI_created_at',
  })
  created_at: Date;

  @Column({
    name: 'PI_updated_at',
  })
  updated_at: Date;

  @Column({
    name: 'PI_size',
  })
  size: number;
}

So I have a the following model : Note that the User logic is not implemented yet. enter image description here

I have a very tiny controller to get albums, I would like to retrieve every albums and every pictures inside it :

import { Request, Response } from 'express';
import { getRepository } from 'typeorm';

import { AlbumModel } from './album.model';

class Album {
  public static async getAlbums(_req: Request, res: Response): Promise<Response> {
    try {
      const picture = await getRepository(AlbumModel).find({ relations: ['pictures'] });
      return res.json(picture);
    } catch (e) {
      console.error(e);
      return res.json('fail');
    }
  }
}

export default Album;

When I call this controller I'm getting the following error : However joinColumns is specified correctly

TypeError: Cannot read property 'joinColumns' of undefined
    at /<something>/src/query-builder/SelectQueryBuilder.ts:1500:61
    at Array.map (<anonymous>)
    at SelectQueryBuilder.createJoinExpression (/<something>/src/query-builder/SelectQueryBuilder.ts:1470:57)
    at SelectQueryBuilder.getQuery (/<something>/src/query-builder/SelectQueryBuilder.ts:54:21)
    at SelectQueryBuilder.QueryBuilder.getQueryAndParameters (/<something>/src/query-builder/QueryBuilder.ts:415:28)
    at SelectQueryBuilder.<anonymous> (/<something>/src/query-builder/SelectQueryBuilder.ts:1956:40)
    at step (/<something>/node_modules/tslib/tslib.js:139:27)
    at Object.next (/<something>/node_modules/tslib/tslib.js:120:57)
    at /<something>/node_modules/tslib/tslib.js:113:75

Any ideas ?

1 Answers

I found the answer, the problem came from the Album entity. The namings was a bit confusing for me. Note the part :

    joinColumn: {
      name: 'PI_id',
      referencedColumnName: 'id',
   }

name is the column name in database

referencedColumnName is the "column" / object property of your entity

import {
  Entity, PrimaryGeneratedColumn, Column, JoinTable, ManyToMany,
} from 'typeorm';
import { PicturesModel } from '../pictures/pictures.model';

@Entity({
  name: 'T_ALBUM_AL',
  synchronize: true,
})
export class AlbumModel {
  @PrimaryGeneratedColumn({
    name: 'AL_id',
  })
  id: number;

  @Column({
    name: 'AL_name',
  })
  name: string;

  @Column({
    name: 'AL_created_at',
  })
  created_at: Date;

  @Column({
    name: 'AL_updated_at',
  })
  updated_at: Date;

  @ManyToMany(() => PicturesModel, (picture: PicturesModel) => picture.id)
  @JoinTable({
    name: 'TJ_PICTURE_ALBUM_PA',
    joinColumn: {
      name: 'PI_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'PI_id',
      referencedColumnName: 'id',
    },
  })
  pictures: PicturesModel[]
}

At the end this gives me the desired output :

{
    "id":1,
   "name":"aa",
   "created_at":"2020-03-16T23:00:00.000Z",
   "updated_at":"2020-03-16T23:00:00.000Z",
   "pictures":[
      {
         "id":1,
         "name":"Pic 1",
         "path":"Path",
         "created_at":"2020-03-17T23:00:00.000Z",
         "updated_at":"2020-03-17T23:00:00.000Z",
         "size":"2"
      }
   ]
}
Related