U
U
urajo2020-04-20 14:35:31
Node.js
urajo, 2020-04-20 14:35:31

How to JOIN in sequelize?

I need to display contacts from the data table if there is an intersection of left and main, how to do this? Models

const Sequelize = require('sequelize'),
    sequelize = new Sequelize('directory','direc','9696Gory!',{
        dialect: 'mysql',
        host: '45.84.255.226'
    }),
    left = sequelize.define('left',{
        id:{
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        },
        name:{
            type: Sequelize.STRING,
            allowNull: false
        }
    },{
        timestamps: false,
        freezeTableName: true
    }),
    main = sequelize.define('main',{
        id:{
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        },
        name:{
            type: Sequelize.STRING,
            allowNull: false
        }
    },{
        timestamps: false,
        freezeTableName: true
    }),
    data = sequelize.define('data',{
        id:{
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        },
        function:{
            type: Sequelize.STRING,
            allowNull: false
        },
        name:{
            type: Sequelize.STRING,
            allowNull: false
        },
        cabinet:{
            type: Sequelize.STRING,
            allowNull: false
        },
        phone:{
            type: Sequelize.STRING,
            allowNull: false
        }
    },{
        timestamps: false,
        freezeTableName: true
    }),
    m_l = sequelize.define('m_l',{
        id:{
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        }
    },{
        timestamps: false,
        freezeTableName: true
    }),
    l_d = sequelize.define('m_d',{
        id:{
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        }
    },{
        timestamps: false,
        freezeTableName: true
    });

main.belongsToMany(left,{through: m_l});
left.belongsToMany(main,{through: m_l});

data.belongsToMany(left,{through: l_d});
left.belongsToMany(data,{through: l_d});

module.exports = {
    left,
    main,
    data,
    m_l,
    l_d
}

routes
const express = require('express'),
    router = express.Router(),
    models = require('../models');

router.post('/view-content',(request,response)=>{
    models.db.data.findAll({
        include:[{
            model: models.db.left,
            where: {
                id: request.body.left
            },
            include:[{
                model: models.db.main,
                where: {
                    id: request.body.main
                }
            }]
        }]
    })
        .then(all=>{

        })
        .catch(err=>console.log(err));
});

module.exports = router;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question