'query with multiple associations
i have this model fight that has 2 associations with another model Player
module.exports = (sequelize: Sequelize) => {
const fight = sequelize.define(
'fight',
{
id: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
},
bet: {
type: DataTypes.FLOAT,
defaultValue: null
}
},
{
paranoid: true,
},
);
fight.belongsTo(sequelize.models.player, {
as: 'player1'
})
fight.belongsTo(sequelize.models.player, {
as: 'player2'
})
};
and i want an endpoint that gets all the fights with the username form player i am trying this code
let fights = await Fight.findAll({
include: [
{
model: Player,
},
{
model: Player,
as: 'player1',
include: [
{
model: Player
}
]
},
{
model: Player,
as: 'player2',
include: [
{
model: Player
}
]
}
]
})
and when i tested it i got this msg
"message": "player is associated to fight multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include."
how to fix it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
