我有一个用户模型,其中包含我的应用程序的所有用户信息。
// model for the users table
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
displayName: {
type: DataTypes.STRING
},
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
}, {
classMethods: {
associate: function(models) {
User.hasMany(models.Session); // adds key to Sessions for UserID
User.hasMany(models.Session, {as: "TeammateId"}); // adds key to Sessions as
},
}
});
return User;
};
该应用程序涉及与队友一起完成任务。我有另一个模型“Sessions”,它跟踪每次尝试完成任务的数据。在 Sessions 的每条记录中,我都将 UserID 作为一列,以及在 session 期间与用户配对的任何人的 TeammateID。我的 session 模型如下:
// model for the sessions table
module.exports = function(sequelize, DataTypes) {
var Session = sequelize.define("Session", {
success: {
type: DataTypes.BOOLEAN,
allowNull: false,
default: false
},
TeammateId: {
type: DataTypes.INTEGER,
allowNull: false
}
}, {
classMethods: {
associate: function(models) {
Session.belongsTo(models.User, {
onDelete: "cascade",
foreignKey: {
allowNull: false
}
});
}
}
});
return Session;
};
我正在尝试编写一个查询,该查询将从 session 中返回一条记录(使用 UserID 作为查找键),但也会包含队友的信息。如何设置/修复我的关联,以便包含队友的信息(使用与用户模型连接的 session 中的 TeammateID)?
我尝试的查询如下:
app.get("[omitted]", function(req, res){
var userId = req.params.userId;
db.Session.findAll({
where: {
UserId: userId
},
include: [db.User] // this is where i am lost. it only returns info for the User and not the Teammate
}).then(function(data){
res.json(data);
})
});
最佳答案
我找到了答案。关键不在于我如何进行必要的关联,而在于编写查询的“包含”部分。这是我的工作代码:
用户模型:
// model for the users table
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
displayName: {
type: DataTypes.STRING
},
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
}
}, {
classMethods: {
associate: function(models) {
User.hasMany(models.Session);
User.hasMany(models.Session, {
as: "Teammate"
});
},
}
});
return User;
};
session 模型:
// model for the Sessions table
module.exports = function(sequelize, DataTypes) {
var Session = sequelize.define("Session", {
success: {
type: DataTypes.BOOLEAN,
allowNull: false,
default: false
}
}, {
classMethods: {
associate: function(models) {
Session.belongsTo(models.User, {
as: "User",
onDelete: "cascade",
foreignKey: {
allowNull: false
}
});
Session.belongsTo(models.User, {
as: "Teammate",
onDelete: "cascade",
foreignKey: {
allowNull: false
}
});
}
}
});
return Session;
};
查询:
// route for returning session history with user and teammate information
app.get("[ommitted]", function(req, res){
var userId = req.params.userId;
db.Session.findAll({
where: {
UserId: userId
},
include: [{
model: db.User,
as: "User"
}, {
model: db.User,
as: "Teammate"
}]
}).then(function(data){
// to do: parse the results into a summary easily consumed by the front end.
res.send(data);
})
});
关于javascript - Sequelize.js - 如何通过另一个模型中的两个键关联模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41992687/