Within my Express application, I am attempting to establish a many-to-many association between two models: User
and Game
, using the intermediary model GamePlayer
. Following guidance from the Sequelize Docs, I utilized the .belongsToMany()
method on both User
and Game
. Unfortunately, upon implementing this association, my application encounters the error message:
GameScore.belongsTo called with something that's not a subclass of Sequelize.Model
. The scenario also involves a GameScore
model linked to Game
, where each Game
corresponds to one GameScore
. Despite experimenting with adding a references
object in the GameScore
model and mixing in .hasMany()
within GamePlayer
along with .belongsTo(GamePlayer)
statements in both User
and Game
, the issue persists. Removal of the association resolves the crashing problem, leaving me puzzled as to whether I'm misunderstanding the process at hand or overlooking crucial details. Any insights offered would be immensely appreciated!
The Sequelize version currently employed is 6.3.3
.
Beneath lies pertinent code snippets from all interconnected models (imports and interface declarations omitted):
User Model
export default class User extends Model { /* ... solely variable declarations */ }
User.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
username: {
type: DataTypes.STRING(50),
allowNull: false,
},
email: {
type: DataTypes.STRING(255),
allowNull: false,
},
password: {
type: DataTypes.STRING(255),
allowNull: false,
},
deleted: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
}, {
tableName: 'users',
sequelize: db,
underscored: true,
});
User.belongsToMany(Game, {
through: GamePlayer,
});
Game Model
export default class Game extends Model { /* ... solely variable declarations */ }
Game.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
deleted: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
}, {
tableName: 'games',
sequelize: db,
underscored: true,
});
Game.hasOne(GameScore);
Game.belongsToMany(User, {
through: GamePlayer,
});
GamePlayer Model
export default class GamePlayer extends Model { /* ... solely variable declarations */ }
GamePlayer.init({
gameId: {
type: DataTypes.UUID,
references: {
model: Game,
key: 'id',
}
},
userId: {
type: DataTypes.UUID,
references: {
model: User,
key: 'id',
}
},
team: {
type: DataTypes.TINYINT({
length: 1,
unsigned: true,
}),
allowNull: false,
},
win: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
}, {
tableName: 'game_players',
sequelize: db,
underscored: true,
});
GameScore Model
export default class GameScore extends Model { /* ... solely variable declarations */ }
GameScore.init({
gameId: {
type: DataTypes.UUID,
allowNull: false,
},
scoreTeam1: {
type: DataTypes.TINYINT({
length: 1,
unsigned: true,
}),
allowNull: false,
},
scoreTeam2: {
type: DataTypes.TINYINT({
length: 1,
unsigned: true,
}),
allowNull: false,
},
}, {
tableName: 'game_scores',
sequelize: db,
underscored: true,
});
GameScore.belongsTo(Game, {
foreignKey: 'gameId',
});