【问题标题】:Sequelize association: associates续集关联:关联
【发布时间】:2022-01-23 05:40:05
【问题描述】:

请我是续集的新手,我有两个模型一直在尝试关联。 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' ],
         
            
          }
        ]
      });

输出

[
  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 数据库中存在 2 的 id。 请问发生这种情况的任何原因

【问题讨论】:

    标签: mysql node.js sequelize.js one-to-many model-associations


    【解决方案1】:

    试试这个 文档: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/ id_company_billing 字段在表 visitorsuite_plans 上不存在。它必须被创建

    【讨论】:

      猜你喜欢
      • 2017-05-30
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多