Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda