mysql - 在 sequelize 中,findAll 和 Join Table with belongsToMany
问题描述
在我的项目中,我有一个名为 Gathering 的表,并且该表引用或引用了多个表。
这些关系可以在下面的代码中找到。
此外,每个关系想要表示的内容在下面的代码中进行了注释。
static associate(db) {
db.Gathering.belongsToMany(db.User, { // Does a user want to set bookmark on this gathering?
as: 'Bookmark',
through: 'Bookmark_Gathering',
foreignKey: 'gathering_idx',
otherKey: 'user_idx',
timestamps: true,
paranoid: true,
});
db.Gathering.belongsToMany(db.User, { // Does a User like this gathering?
as: 'Like',
through: 'Like_Gathering',
foreignKey: 'gathering_idx',
otherKey: 'user_idx',
timestamps: true,
paranoid: true,
});
db.Gathering.belongsToMany(db.User, { // Would a user submit a join application to a gathering?
as: 'Submmit',
through: 'Submmit_Gathering',
foreignKey: 'gathering_idx',
otherKey: 'user_idx',
timestamps: true,
paranoid: true,
});
db.Gathering.belongsToMany(db.User, { // Does a user join in a gathering?
as: 'Join',
through: 'Join_Gathering',
foreignKey: 'gathering_idx',
otherKey: 'user_idx',
timestamps: true,
paranoid: true,
});
db.Gathering.belongsToMany(db.Star, { // How many ratings did that person give to this meeting?
through: 'Star_Gathering',
foreignKey: 'gathering_idx',
otherKey: 'star_idx',
timestamps: true,
paranoid: true,
});
db.Gathering.belongsTo(db.Category, { foreignKey: 'category_idx', targetKey: 'idx'});
db.Gathering.belongsTo(db.Location, {foreignKey: 'location_idx', targetKey: 'idx'});
db.Gathering.belongsTo(db.User, {foreignKey: 'user_idx', targetKey: 'idx'}); // Who made this gathering??
db.Gathering.hasMany(db.Comment, {foreignKey: 'gathering_idx', sourceKey: 'idx'});
}
在这种情况下,我想获得以下响应值。
MeetIdx、meetImgUrl、maxNum、meetTitle 和 meetDate可以在 Gathering 表中获取。
CurNum表示当前参加本次会议的人数,与User_Gathering表为Join相关。
星星当前代表该组的总评分,这与名为Star_Gathering的表相关。
LikeCnt是现在喜欢这个会议的人数,它与Star_Gathering表相关为Like。
"list": [
{
"meetIdx": 0,
"meetImgUrl": "String",
"categoryName": "String",
"maxNum": 0,
"curNum": 0,
"meetTitle": "String",
"meetDate": "String",
"stars": 0,
"likeCnt": 0
}
]
我试过这个来获得喜欢的数量。
await Gathering.findAll({
group: ['gathering.idx'],
include: [
{
model: sequelize.Like,
attributes: [],
}
],
attributes: [
'idx',
'title',
'date',
'time',
'content',
'maxnum',
[
sequelize.fn('count', sequelize.col('like.gathering_idx')), 'likes'
],
],
})
但是我收到了这个错误信息。
0|app | 21-03-23 13:27:38: Error: Include unexpected. Element has to be either a Model, an Association or an object.
0|app | at Function._conformInclude (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:383:11)
0|app | at options.include.options.include.map.include (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:319:59)
0|app | at Array.map (<anonymous>)
0|app | at Function._conformIncludes (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:319:39)
0|app | at Function._baseMerge (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:786:10)
0|app | at Function._defaultsOptions (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:823:17)
0|app | at Function._injectScope (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:3236:10)
0|app | at Function.findAll (/home/ec2-user/mepus_test/node_modules/sequelize/lib/model.js:1706:10)
0|app | at sequelize.transaction (/home/ec2-user/mepus_test/routes/Gathering/gathering.ctrl.js:134:46)
0|app | at Sequelize._clsRun (/home/ec2-user/mepus_test/node_modules/sequelize/lib/sequelize.js:1090:30)
0|app | 21-03-23 13:27:38: Executing (6fe0251a-17f7-4a53-9a6e-6d5a7ce321f8): ROLLBACK;
0|app | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
0|app | Error: Transaction cannot be committed because it has been finished with state: rollback
0|app | at Transaction.commit (/home/ec2-user/mepus_test/node_modules/sequelize/lib/transaction.js:56:13)
0|app | at Sequelize._clsRun (/home/ec2-user/mepus_test/node_modules/sequelize/lib/sequelize.js:1091:27)
0|app | at process._tickCallback (internal/process/next_tick.js:68:7)
因为我首先处理的是 Sequelize,所以我无法理解问题所在。
请让我知道如何解决其中的一些问题。
解决方案
推荐阅读
- iframe - 可以在不使用 iframe 的情况下嵌入其他网站(例如 wordpress)中的 Blazor 服务器端应用程序
- python - Update column for last row of first group
- function - 函数中不支持的操作数类型
- html - 将电子邮件正文部分加粗
- webpack - webpack中publicPath和contentBase的区别
- r - R ggplot2人口金字塔x和y轴消失
- java - 点击事件抛出过时的元素引用:元素未附加到页面文档
- amazon-s3 - 没有为流数据指定内容长度。流内容将在内存中缓冲,并可能导致内存不足错误
- java - 二进制 XML 文件第 41 行:膨胀类 com.google.android.material.textfield.TextInputLayout 时出错
- postgresql - 尝试创建物化视图时在“Z”处或附近出现语法错误