{ var MasterPu-6ren">
gpt4 book ai didi

mysql - 从sequelize关联检索值时出错

转载 作者:行者123 更新时间:2023-11-29 19:30:13 26 4
gpt4 key购买 nike

我需要从现有数据库中检索两个关联模型的值。

模型大师

"use strict";

module.exports = (sequelize, DataTypes) => {
var MasterPurchaseOrder = sequelize.define('master_purchase_order' , {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id'
},
requestedById: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'requested_by_id'
},
status: {
type: DataTypes.STRING,
allowNull: true,
field: 'status'
}
},{
classMethods:{
associate: (models) =>{
MasterPurchaseOrder.hasMany(models.purchase_order,{
as: 'purchase_order',
foreignkey: 'master_purchase_order_id'
});
},
},
}, {
timestamps: true,
paranoid: false,
underscored: true,
freezeTableName: true,
tableName: 'master_purchase_orders'
});

return MasterPurchaseOrder;
}

采购订单模型

"use strict";

module.exports = (sequelize, DataTypes) => {
var PurchaseOrder = sequelize.define('purchase_order' , {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id'
},
purchaseDate: {
type: DataTypes.DATE,
allowNull: true,
field: 'purchase_date'
},
requestedById: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'requested_by_id'
},
masterPurchaseOrderId: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'master_purchase_order_id'
}
},{
classMethods:{
associate: (models) => {
PurchaseOrder.belongsTo(models.master_purchase_order,{
as: 'master_purchase_order',
foreignkey: 'master_purchase_order_id'
});
},
},
}, {
timestamps: true,
paranoid: false,
underscored: true,
freezeTableName: true,
tableName: 'purchase_orders'
});

return PurchaseOrder;
}

这是我的采购订单 Controller :

"using strict";
const purchaseOrder = require('../models/').purchase_order;
const MasterPurchaseOrder = require('../models/').master_purchase_order;

module.exports= {
index(req, res) {
purchaseOrder
.findAll({
include:[
{model: MasterPurchaseOrder, as: 'master_purchase_order'}
]
})
.then((masters) => {
res.status(200).json(masters);
});
// .catch((error) => {
// res.status(500).json(error);
// });
},

};

我收到错误:

Unhandled rejection Error: master_purchase_order (master_purchase_order) is not associated to purchase_order!

任何建议

最佳答案

我重写了子模型中的 classMethods 定义。

{
classMethods:{
associate: (models) => {
PurchaseOrder.belongsTo(models.master_purchase_order,{
as: 'master_purchase_order',
foreignkey: 'master_purchase_order_id'
});
associate: (models) => {
PurchaseOrder.belongsTo(models.suuplier,{
as: 'supplier',
foreignkey: 'supplier_id'
});
},
},
}

正确的做法是:

{
classMethods:{
associate: (models) => {
PurchaseOrder.belongsTo(models.master_purchase_order,{
as: 'master_purchase_order',
foreignkey: 'master_purchase_order_id'
});
PurchaseOrder.belongsTo(models.suuplier,{
as: 'supplier',
foreignkey: 'supplier_id'
});
},
}

关于mysql - 从sequelize关联检索值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com