sequelize计数不适用于嵌套对象

时间:2019-06-14 04:59:36

标签: node.js express sequelize.js

pInvent.js

module.exports = (sequelize, DataTypes) => {
    const PInvent = sequelize.define('prod_invent', {
        id: {primaryKey: true, type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, unique: true},
        sku_id: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        }
    });

    PInvent.associations = (db) => {
        PInvent.belongsToMany(db.cTags, {
            as: 'CTags',
            through: 'c_prod',
            foreignKey: 'prod_id',
            otherKey: 'c_id'
        });
    };

    return PInvent
}

pCure.js

module.exports = (sequelize, DataTypes) => {
    const PCure = sequelize.define('prod_cure', {
            id: {primaryKey: true, type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, unique: true},
            c_tag: {type: DataTypes.STRING, allowNull: false, unique: true}

        });

    PCure.associations = (db) => {
        PCure.belongsToMany(db.pInvent, {
            as: 'CProd',
            through: 'c_prod',
            foreignKey: 'c_id',
            otherKey: 'prod_id'
        });
    };


    return PCure
}

db.js

db.pInvent = require('../model/pInvent')(sequelize, Sequelize);
db.cTags = require('../model/pCure')(sequelize, Sequelize);


db.cTags.associations(db);

现在,当我编写如下所示的代码时:

db.cTags.findAll({
            attributes: ['prod_cure.*', 'prod_invent.*', [Sequelize.fn('COUNT', Sequelize.col('prod_invent.id')), 'PostCount']],
            include: [{
                model: db.pInvent, as: 'CProd'
            }]
        }).then((data) => {
            res.json({info:data})
        }).catch((err) => {
            res.json({errInfo:err})
        })

它给了我如下所示的错误:

未知表'prod_invent'

我想要的是,我想计算嵌套对象的数量,但是我无法做到。

0 个答案:

没有答案