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

150
Visualizações
Two foreign key one table in sequalize

I have Offers table 2 row exit_customs and destination_customs. This 2 row has a my Customs table id. My problem is how to two(exit_customs and destination_customs) foreign key one(Customs) table?

Here is my list query function

router.post('/api/logistic/offers/get-offers', async (req, res) => {
    const {limit, page, sortColumn, sortType, search} = req.body;
    const total = await Offers.findAll();
    const offersList = await Offers.findAll({
        limit: limit,
        offset: (page - 1) * limit,
        order: [
            [sortColumn, sortType]
        ],
        where: {
            [Op.or]:[
                {
                    offers_no: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    agreement_date: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    routes: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    type_of_the_transport: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
            ]
        }
    });
    res.json({
        total: total.length,
        data: offersList
    });
})
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

this solution connects the two data in the first table to the id in the other table as you want. hope it helps

Offers.belongsTo(Customs, {as: 'Exit_customs', foreignKey: 'exit_customs'});
Offers.belongsTo(Customs, {as: 'Destination_customs', foreignKey: 'destination_customs'});
router.post('/api/logistic/offers/get-offers', async (req, res) => {
    const {limit, page, sortColumn, sortType, search} = req.body;
    const total = await Offers.findAll();
    const offersList = await Offers.findAll({
        limit: limit,
        offset: (page - 1) * limit,
        order: [
            [sortColumn, sortType]
        ],
        where: {
            [Op.or]: [
                {
                    offers_no: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    agreement_date: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    routes: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
                {
                    type_of_the_transport: {
                        [Op.substring]: [
                            search
                        ]
                    }
                },
            ]
        },
        include: [{
            model: Customs,
            as: 'Exit_customs'
        }, {
            model: Customs,
            as: 'Destination_customs'

        }]
    });
    res.json({
        total: total.length,
        data: offersList
    });
})

about 4 years ago · Santiago Trujillo Relatório

0

You can give foreign key using "as" keyword, please add below code in your Offers models file

static associate(models) {
      models.Offers.belongsTo(models.Customs, {
        foreignKey: "exit_customs",
        as: "exitCustomsDetails",
      });

      models.Offers.belongsTo(models.Customs, {
        foreignKey: "destination_customs",
        as: "destinationCustomsDetails",
      });
}
about 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