Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Sequelize simple inner join

I have two models, policyTable and policy_rule defined as below. There is a column called policy_id in policy_rule which is a foreign key and references to id column in policyTable. The policy_rule can have multiple policyTable, i.e there is a 1:N relationship.

var policyTable = dbController.db.define('policyTable', {
    id: {
        type: Sequelize.BIGINT,
        autoIncrement: true,
        primaryKey: true
    },
    't': {
        type: Sequelize.BIGINT,
        unique: true
    },
    'name': {
        type: Sequelize.STRING,
    },
    src: {
        type: Sequelize.STRING
    }
}, {
    timestamps: false,
    freezeTableName: true,
    tableName: 'zo_policy',
});

var policy_rule = dbController.db.define('policy_rule', {
    policy_id: {
        type: Sequelize.BIGINT,
        references: {
            model: policyTable,
            key: 'id'
        },
        onUpdate: 'cascade',
        onDelete: 'cascade'
    },
    agent_id: {
        type: Sequelize.BIGINT,
    },
    enabled: {
        type: Sequelize.BOOLEAN,
    }
}, {
    timestamps: false,
    freezeTableName: true,
    tableName: 'zo_policy_rule',
});

Now I want to join these two models and get all columns of both of them. How can I do that? I tried the below code but it says

Error: policy_rule is not associated to policyTable!

function getAllPolicies() {
    return policyTable.findAndCountAll({
        include: [{
            model: policy_rule
            }]
    }).then(function (users) {
        console.log(users);
        data.count = users.count;
        data.users = users.rows;
        console.log(data);
        return data;
    });
};
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Even though your associations are defined at the database level, and you indicate this in your column definitions, to use sequelize's include feature, you still need to explicitly define your associations between models, too.

Here's the relevant section in the doco: http://docs.sequelizejs.com/en/latest/docs/associations/#belongsto and http://docs.sequelizejs.com/en/latest/docs/associations/#1m

In your case, it'll mean adjusting your code to something along the lines of below - note the new classMethods section in each model definition:

var policyTable = dbController.db.define('policyTable', {
    id: {
        type: Sequelize.BIGINT,
        autoIncrement: true,
        primaryKey: true
    },
    't': {
        type: Sequelize.BIGINT,
        unique: true
    },
    'name': {
        type: Sequelize.STRING,
    },
    src: {
        type: Sequelize.STRING
    }
}, {
    timestamps: false,
    freezeTableName: true,
    tableName: 'zo_policy',
    classMethods: {
      associate(models) {
        this.hasMany(models.policy_rule, {foreignKey: 'policy_id'})
      }
    }
});

var policy_rule = dbController.db.define('policy_rule', {
    policy_id: {
        type: Sequelize.BIGINT,
        references: {
            model: policyTable,
            key: 'id'
        },
        onUpdate: 'cascade',
        onDelete: 'cascade'
    },
    agent_id: {
        type: Sequelize.BIGINT,
    },
    enabled: {
        type: Sequelize.BOOLEAN,
    }
}, {
    timestamps: false,
    freezeTableName: true,
    tableName: 'zo_policy_rule',
    classMethods: {
      associate(models) {
        this.belongsTo(models.policyTable, {foreignKey: 'policy_id'})
      }
    }
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!