Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda