繁体   English   中英

续集关联:关联

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM