Ask questionsUnable to get many to many working

Been trying to get my Many to Many association to work, but unable to do so. I looked at the example code, but it's not working for me.

// user.model.ts
import { Table, Model, Column, BelongsToMany } from 'sequelize-typescript';
import { Room, UserRoom } from './index';

class User extends Model<User> {
    id: number;

    spotify_id: number;

    @BelongsToMany(() => Room, () => UserRoom)
    rooms: Room[];

export default User;
// room.model.ts
import { Table, Model, Column, BelongsToMany } from 'sequelize-typescript';
import { User, UserRoom } from './index';

class Room extends Model<Room> {
    id: number;

    name: string;

    image: string;

    @BelongsToMany(() => User, () => UserRoom)
    users: User[];

export default Room;
// user_room.model.ts
import { Table, Model, Column, ForeignKey, PrimaryKey } from 'sequelize-typescript';
import { Room, User } from './index';

class UserRoom extends Model<UserRoom> {
    @ForeignKey(() => User)
    user_id: number;

    @ForeignKey(() => Room)
    room_id: number;

export default UserRoom;

This results in the following error:

throw new Error("Foreign key for \"" + + "\" is missing " +
Error: Foreign key for "Room" is missing on "User_Room".

Is there something I'm missing? It's basically a 1-1 copy of the example code.


Answer questions kfrajtak

I am experiencing different problem with many-to-many:

@BelongsToMany(() => Role, () => UserRole) roles: Role[];

The error is

(node:41692) UnhandledPromiseRejectionWarning: TypeError: Cannot read property
'prototype' of undefined
    at Sequelize.model (...\node_modules\sequelize-typescript\dist\sequelize\

Via poor man's debugger (i.e. console) I get

BelongsToManyAssociation {
  associatedClassGetter: [Function],
   { foreignKey: undefined,
     otherKey: undefined,
     through: [Function],
     as: 'roles' } }

and the through function is returning undefined.

Can anybody help?

Thanks, Karel

Github User Rank List