首页 > 解决方案 > 在 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,所以我无法理解问题所在。

请让我知道如何解决其中的一些问题。

标签: mysqlnode.jssequelize.js

解决方案


推荐阅读