Sequelize belongsToMany不工作

时间:2018-04-12 18:12:01

标签: mysql angularjs node.js sequelize.js

这是我的问题: 表WO - > sparepart_request - >零部件。 工单有几个备件,备件可以属于几个WO。

这是我在wo.js(续集模型)中的代码

models.sparepart.belongsToMany(models.wo, { as: 'SPWO', through: 'sparepart_request', foreignKey: 'codSparePart' });

这是我在sparepart.js(续集模型)中的代码。

models.sparepart.belongsToMany(models.wo, { as: 'SPWO', through: 'sparepart_request', foreignKey: 'codSparePart' });

在sparepart_request中没有关于关联的信息。我已按照下一条说明Sequelize

进行操作

在我的查询中,我有下一个代码:

exports.readDetailWO = function (req, res) {
models.wo.findAll({
    attributes: ['codWO'], // attributes: ['id', 'codWO', 'codSparePart', 'quantity', 'date_request', 'date_reception', 'details', 'codUser', 'received'],
    raw: true,
    where: {
        codWO: req.params.codWO
    },
    include: [{
        model: models.sparepart,
        attributes: ['codSparePart', 'name', 'description', 'codManufacturer', 'image_uri', 'stock'],
        paranoid: false,
        required: false,
        as: 'SPWO'
    }]
}).then(sparePart => {
    if (!sparePart) {
        res.status(404);
        res.send({
            success: false,
            message: 'Spare Part not found. ' + req.params.codWO,
            data: sparePart
        });
    } else if (sparePart) {
        res.json({
            success: true,
            message: 'Spare Part found.',
            data: sparePart
        });
    }
}).catch(function (error) {
    logger.error(JSON.stringify(error));
    res.json({
        message: 'Query not successful and error has occured reading',
        error: error,
        stackError: error.stack
    });
    return res.status(500);
});

};

但服务器的响应(使用PostMan)如下:

{
"message": "Query not successful and error has occured reading",
"error": {
    "name": "SequelizeEagerLoadingError"
},
"stackError": "SequelizeEagerLoadingError: sparepart is not associated to wo!\n 

我已经在这里读到,可能我的primaryKeys问题不是名称ID,但现在我可以更改这些名称......

问题出在哪里?在此先感谢您的帮助。

model sparepart_request.js

module.exports = function (sequelize, DataTypes) {
var sparepart_request = sequelize.define('sparepart_request', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    codWo: {
        type: DataTypes.STRING(20),
        allowNull: false,
        foreignKey: {
            model: 'wo',
            key: 'codWO'
        }
    },
    codSparePart: {
        type: DataTypes.STRING(30),
        allowNull: false,
        references: {
            model: 'sparepart',
            key: 'codSparePart'
        }
    },
    quantity: {
        type: DataTypes.FLOAT,
        allowNull: true
    },
    date_request: {
        type: DataTypes.DATEONLY,
        allowNull: true
    },
    date_reception: {
        type: DataTypes.DATEONLY,
        allowNull: true
    },
    details: {
        type: DataTypes.TEXT,
        allowNull: true
    },
    codUser: {
        type: DataTypes.STRING(20),
        allowNull: false,
        references: {
            model: 'user',
            key: 'codUser'
        }
    },
    received: {
        type: DataTypes.INTEGER(1),
        allowNull: false
    }
}, {
    tableName: 'sparepart_request',
    timestamps: false
});

/* sparepart_request.associate = function (models) {
    models.sparepart_request.hasMany(models.sparepart, {foreignKey: 'codSparePart', targetKey: 'codSparePart'});
}; */
return sparepart_request;

};

模特wo.js:

 /* jshint indent: 1 */

module.exports = function (sequelize, DataTypes) {
var wo = sequelize.define('wo', {
    codWO: {
        type: DataTypes.STRING(20),
        allowNull: false,
        primaryKey: true
    },
    codUser: {
        type: DataTypes.STRING(20),
        allowNull: false,
        references: {
            model: 'user',
            key: 'codUser'
        }
    },
    codOriginator: {
        type: DataTypes.STRING(20),
        allowNull: true,
        references: {
            model: 'user',
            key: 'codUser'
        }
    },
    capture_date: {
        type: DataTypes.DATE,
        allowNull: false
    },
    active: {
        type: DataTypes.INTEGER(1),
        allowNull: false
    },
    codType: {
        type: DataTypes.CHAR(3),
        allowNull: false,
        references: {
            model: 'type',
            key: 'codType'
        }
    },
    date: {
        type: DataTypes.DATEONLY,
        allowNull: false
    },
    title: {
        type: DataTypes.STRING(255),
        allowNull: true
    },
    date_finish: {
        type: DataTypes.DATEONLY,
        allowNull: true
    },
    codStatus: {
        type: DataTypes.STRING(10),
        allowNull: false,
        references: {
            model: 'status',
            key: 'codStatus'
        }
    },
    hours_planned: {
        type: DataTypes.FLOAT,
        allowNull: true
    },
    codElement: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        references: {
            model: 'element',
            key: 'codElement'
        }
    },
    Security: {
        type: DataTypes.INTEGER(1),
        allowNull: true,
        defaultValue: '0'
    },
    codEquipment: {
        type: DataTypes.STRING(20),
        allowNull: false,
        references: {
            model: 'equipment',
            key: 'codEquipment'
        }
    },
    codProject: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        references: {
            model: 'project',
            key: 'id'
        }
    },
    codTestRoom: {
        type: DataTypes.STRING(10),
        allowNull: false,
        references: {
            model: 'testroom',
            key: 'codTestRoom'
        }
    }
}, {
    tableName: 'wo',
    timestamps: false
});

wo.associate = function (models) {
    models.wo.belongsTo(models.wo_operation, {
        as: 'wo_operation',
        foreignKey: {
            name: 'codWO',
            allowNull: false
        },
        targetKey: 'codWO'
    });
    models.wo.belongsTo(models.dailyinfo_detail, {
        as: 'dailyInfo',
        foreignKey: {
            name: 'codWO',
            allowNull: false
        },
        targetKey: 'codWO'
    });
    models.wo.hasOne(models.wo_corrective, {
        as: 'wo_corrective',
        foreignKey: {
            name: 'codWO',
            allowNull: false
        },
        targetKey: 'codWO'
    });
    models.wo.hasOne(models.wo_preventive, {
        as: 'wo_preventive',
        foreignKey: {
            name: 'codWO',
            allowNull: false
        },
        targetKey: 'codWO'
    });
    models.wo.belongsToMany(models.sparepart_request, { as: 'WOSP', through: 'sparepart_request', foreignKey: 'codWO', otherKey: 'codSparePart' });
};    
return wo;

};

型号sparepart.js

/* jshint indent: 1 */

module.exports = function (sequelize, DataTypes) {
var sparepart = sequelize.define('sparepart', {
    codSparePart: {
        type: DataTypes.STRING(30),
        allowNull: false,
        primaryKey: true,
        references: {
            model: 'sparepart_request',
            key: 'codSparePart'
        }
    },
    name: {
        type: DataTypes.STRING(45),
        allowNull: true
    },
    description: {
        type: DataTypes.TEXT,
        allowNull: true
    },
    available: {
        type: DataTypes.INTEGER(1),
        allowNull: false,
        defaultValue: '1'
    },
    codManufacturer: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        references: {
            model: 'manufacturer',
            key: 'codManufacturer'
        }
    },
    stock: {
        type: DataTypes.INTEGER(10),
        allowNull: true
    },
    image_uri: {
        type: DataTypes.STRING(500),
        allowNull: true
    },
    codProject: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        references: {
            model: 'project',
            key: 'id'
        }
    },
    price: {
        type: DataTypes.FLOAT,
        allowNull: false
    }
}, {
    tableName: 'sparepart',
    timestamps: false
});

sparepart.associate = function (models) {
    models.sparepart.belongsTo(models.manufacturer, {
        foreignKey: 'codManufacturer',
        targetKey: 'codManufacturer'
    });

    models.sparepart.belongsToMany(models.wo, { as: 'SPWO', through: 'sparepart_request', foreignKey: 'codSparePart', otherKey: 'codWO' });
};

return sparepart;
};

在这里,您可以找到我的代码,三个模型和查询。目前我使用的是邮递员,我在前端没有任何东西。

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

从sparepart_request中删除id。

在sparepart_request中包含下一个代码:

sparepart_request.associate = function (models) {
    models.sparepart_request.hasMany(models.sparepart, {foreignKey: 'codSparePart', targetKey: 'codSparePart'});
    models.sparepart_request.hasMany(models.wo, {foreignKey: 'codWO', targetKey: 'codWO'});
};

这是正确的方法吗?显然它正在运作......