Brisstone
Brisstone

Reputation: 61

Sequelize association: associates

Please i'm new to sequelize and I have two models I've been trying to associate. The company_billing models contains several plans of different companies while the visitorsuite_plans contains distinct plans. I'm guessing that's one to many. So i want to link the plan column in company_billing to id column in visitorsuite_plans. But when i make the query I get an empty array but a union actually exists.

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: []
    },

Whereas an id of 2 exists on the visitorsuite_plans database. Please Any reasons why this is happening

Upvotes: 1

Views: 1104

Answers (1)

Joe
Joe

Reputation: 64

Try this Doc : 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/ And on your company_billing.js file complete your relationship with this::

company_billing.associate = function(models) {
      company_billing.hasMany(models.visitorsuite_plans, 
      {
        foreignKey: 'id_company_billing',
        sourceKey: 'id',
        as: 'billingPlan',
        constraints: false
  });

3/ The id_company_billing field does not exist on table visitorsuite_plans. It will have to be created

Upvotes: 0

Related Questions