[英]Sequelize association: associates
請我是續集的新手,我有兩個我一直在嘗試關聯的模型。 company_billing 模型包含不同公司的多個計划,而 visitorsuite_plans 包含不同的計划。 我猜這是一對多。 所以我想將company_billing 中的plan 列鏈接到visitorsuite_plans 中的id 列。 但是當我進行查詢時,我得到一個空數組,但實際上存在一個聯合。
company_billing.js
/* jshint indent: 2 */
module.exports = (sequelize, DataTypes) => {
var company_billing= sequelize.define(
'company_billing',
{
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
company: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'visitorsuite_company',
key: 'id'
}
},
amount: {
type: DataTypes.INTEGER(255),
allowNull: false
},
payment_status: {
type: DataTypes.ENUM('paid', 'cancelled'),
allowNull: false
},
plan: {
type: DataTypes.INTEGER(11),
allowNull: true,
defaultValue: null,
references: {
model: 'visitorsuite_plans',
key: 'id'
}
},
transaction_ref: {
type: DataTypes.STRING(255),
allowNull: true,
},
date: {
type: DataTypes.DATE,
allowNull: false
},
period: {
type: DataTypes.ENUM('month', 'year'),
allowNull: true
}
},
{
tableName: 'company_billing'
}
);
company_billing.associate = function(models) {
company_billing.hasMany(models.visitorsuite_plans,
{
foreignKey: 'id',
as: 'billingPlan',
constraints: false
});
}
return company_billing
};
visitorsuite_plans.js
/* jshint indent: 2 */
module.exports = (sequelize, DataTypes) => {
var visitorsuite_plans = sequelize.define(
'visitorsuite_plans',
{
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
plan_name: {
type: DataTypes.STRING(30),
allowNull: false,
unique: true
},
monthly_billing: {
type: DataTypes.DECIMAL(10,2),
allowNull: true
},
yearly_billing: {
type: DataTypes.DECIMAL(10,2),
allowNull: true
},
duration: {
type: DataTypes.INTEGER(11),
allowNull: true
},
is_active: {
type: DataTypes.INTEGER(11),
allowNull: false
}
},
{
tableName: 'visitorsuite_plans'
}
);
visitorsuite_plans.associate = function(models) {
visitorsuite_plans.belongsTo(models.company_billing, {
foreignKey: 'plan',
as: 'billingPlan',
constraints: false
});
// ANother association that exists on the model
visitorsuite_plans.belongsTo(models.visitorsuite_company_plan, {
foreignKey: 'plan',
as: 'planInfo',
constraints: false
});
}
return visitorsuite_plans
};
controller.js
const billings = await company_billing.findAll({
where: {
company: req.user.company
},
include: [
{
model: visitorsuite_plans,
as: 'billingPlan',
attributes: ['id','plan_name' ],
}
]
});
OUTPUT
[
company_billing {
dataValues: {
id: 17,
company: 101,
amount: 350000,
payment_status: 'cancelled',
plan: 2,
transaction_ref: null,
date: 2022-01-19T14:57:43.000Z,
period: 'year',
createdAt: 2022-01-19T14:57:43.000Z,
updatedAt: 2022-01-19T14:57:43.000Z,
deletedAt: null,
billingPlan: []
},
而visitorsuite_plans 數據庫中的id 為2。 請問發生這種情況的任何原因
試試這個文檔: https://sequelize.org/master/manual/eager-loading.html
1/
const billings = await company_billing.findAll({
where: {
company: req.user.company
},
include: [
{
association: 'billingPlan',
attributes: ['id','plan_name' ],
}
]
});
2/在您的company_billing.js文件上完成您與此的關系::
company_billing.associate = function(models) {
company_billing.hasMany(models.visitorsuite_plans,
{
foreignKey: 'id_company_billing',
sourceKey: 'id',
as: 'billingPlan',
constraints: false
});
3/表visitorsuite_plans上不存在id_company_billing字段。 它必須被創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.