You can define match as following.
(您可以按以下方式定义匹配。)
I assume you have UserModel.id
as Seqeulize.INTEGER
. (我假设您有UserModel.id
作为Seqeulize.INTEGER
。)
const MatchModel = Sequelize.define("Match", {
// ... Other column declaration
// Two user column -- which are not linked yet.
playerOneId: {
type: Sequelize.INTEGER,
allowNull: false
},
playerTwoId: {
type: Sequelize.INTEGER,
allowNull: false
}
})
// Now time to link the UserModel
// I assume you have `UserModel` declared before doing this.
MatchModel.belongsTo(UserModel, {
foreignKey: {
name: "playerOneId"
},
as: "playerOne"
}
MatchModel.belongsTo(UserModel, {
foreignKey: {
name: "playerTwoId"
},
as: "playerTwo"
}
Now while query a MatchModel
do following
(现在,在查询MatchModel
请执行以下操作)
MatchModel.findOne({
where: {
// Your condition goes here
},
includes: [
{
model: UserModel,
as: "playerOne"
},
{
model: UserModel,
as: "playerTwo"
},
]
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…