Search code examples
node.jspostgresqlnestjstypeorm

Update request ManyToMany NestJS


I'm quite new on coding, I am using nestjs to create an app along with PostegreSql Whenever I try to update something I get an error.

I have Entities with @ManyToMany relation, If I remove these relations the Update works properly. But if I let the @ManyToMany relation, it won't work

Error message:

[Nest] 46879   - 09/21/2019, 7:38 PM   [ExceptionsHandler] column "entrainementsId" of relation "entrainements" does not exist +639401ms
[0] QueryFailedError: column "entrainementsId" of relation "entrainements" does not exist
[0]     at new QueryFailedError (/Users/damien/Desktop/projetAngular/rowing/rowing-back-end/node_modules/typeorm/error/QueryFailedError.js:11:28)
[0]     at Query.callback (/Users/damien/Desktop/projetAngular/rowing/rowing-back-end/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
[0]     at Query.handleError (/Users/damien/Desktop/projetAngular/rowing/rowing-back-end/node_modules/pg/lib/query.js:142:17)
[0]     at Connection.connectedErrorMessageHandler (/Users/damien/Desktop/projetAngular/rowing/rowing-back-end/node_modules/pg/lib/client.js:194:17)
[0]     at Connection.emit (events.js:197:13)
[0]     at Socket.<anonymous> (/Users/damien/Desktop/projetAngular/rowing/rowing-back-end/node_modules/pg/lib/connection.js:126:12)
[0]     at Socket.emit (events.js:197:13)
[0]     at addChunk (_stream_readable.js:288:12)
[0]     at readableAddChunk (_stream_readable.js:269:11)
[0]     at Socket.Readable.push (_stream_readable.js:224:10)

EntrainementEntity


@Entity()
export class Entrainements extends HistoriqueEntity {
    @Column()
    name: string;

    @ManyToMany( () => Categories, {cascade: true} )
    @JoinTable()
    category: Categories[];

    @Column()
    distance: number;

    @Column()
    start: number;

    @Column()
    comments: string;

    @Column()
    warmUp: string;

    @Column()
    cadence: string;

    @ManyToOne(type => Season, season => season.name)
    season: Season;

    @Column()
    rest: string;

    @ManyToOne(type => Roles, role => role.entrainements)
    role: Roles;

    @ManyToMany( () => Exercices, {cascade: true} )
    @JoinTable()
    exercices: Exercices[];
}

ExercicesEntity :

@Entity()
export class Exercices extends HistoriqueEntity {
    @Column()
    name: string;

    @Column()
    description: string;

    @ManyToOne(type => TypeExercices, typeExercices => typeExercices.exercices)
    typeExercices: TypeExercices;

    @ManyToMany(type => Entrainements, entrainements => entrainements.exercices)
    entrainements: Entrainements[];
}

CategoriesEntity


@Entity()
export class Categories extends HistoriqueEntity {
    @Column()
    name: string;

    @ManyToMany(type => Entrainements, entrainements => entrainements.category)
    entrainements: Entrainements[];

}

EntrainementControlleur

    @Put(':id/update')
    async update(@Param('id') id, @Body() entrainementsData: Entrainements): Promise<UpdateResult> {
        entrainementsData.id = Number(id);
        return this.service.updateTraining(entrainementsData);
    }

EntrainementsService

   async updateTraining(entrainement: Entrainements): Promise<UpdateResult> {
        return await this.entrainementsRepository.update(entrainement.id, entrainement);
    }

Solution

  • you should define the opposite binding:

    @ManyToMany( () => Exercices, (exercices) => exercices.entrainements, {cascade: true} )
    @JoinTable()
    exercices: Exercices[];