Tengo la tabla de ofertas de 2 filas exit_customs y destination_customs. Esta fila 2 tiene una identificación de tabla de mi Aduana. Mi problema es cómo dos (exit_customs y destination_customs) una tabla de clave externa (Aduana)?
Aquí está mi función de consulta de lista
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 }); })esta solución conecta los dos datos en la primera tabla con la identificación en la otra tabla como desee. Espero eso ayude
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 }); })Puede proporcionar una clave externa usando la palabra clave "as" , agregue el código a continuación en su archivo de modelos de ofertas
static associate(models) { models.Offers.belongsTo(models.Customs, { foreignKey: "exit_customs", as: "exitCustomsDetails", }); models.Offers.belongsTo(models.Customs, { foreignKey: "destination_customs", as: "destinationCustomsDetails", }); }