Aayushi Gupta
Aayushi Gupta

Reputation: 367

Join or Populate Query In Sequelize

Initially i was using mongoDb to access or manipulate database. But now I want to shift to mySQL but then I used Sequelize ORM to get or set data. But I am having problem in joining or populating 2 tables together to show the data on screen

I have already include attribute to get whole data but I failed

Warehouse.findAll( { raw: true } ).then( warehouses => {
                Location.findAll( { raw: true }, { include: { model: Warehouse } } ).then( locations => {
                    const locationObject = locations.map( loaction => {
                        return Object.assign( {}, {
                            location_code: loaction.location_code,
                            location_name: loaction.location_name,
                            location_created_on: loaction.location_created_on,
                            isActive: loaction.isActive,
                            location_warehouse: warehouses.map( warehouse => {
                                return Object.assign( {}, {
                                    warehouse_code: warehouse.warehouse_code,
                                    warehouse_name: warehouse.warehouse_name,
                                } )
                            } )
                        } )
                    } )
                    console.log( "locations---------------", locations )
                    if ( locations == null )
                        return res.status( 422 ).send( { header: "Error", content: "err-msg.NoData" } );
                    else {
                        Location.count().then( counts => {
                            if ( err ) {
                                res.status( 422 ).send( { header: "Error", content: "err-msg.NoData" } );
                            } else {
                                res.send( {
                                    success: true,
                                    msg: resp = {
                                        locations,
                                        counts
                                    }
                                } );
                            }
                            return;
                        } );
                        return;
                    }
                    res.json( locationObject );
                } );
            } )

schema i used

module.exports = function () {
    const Sequelize = require( 'sequelize' );
    const { db } = require( '../../Config/config' );
    const sequelize = new Sequelize( db.url , '*********',, '*********', {
        host: '*********',,
        port: '3306',
        dialect: 'mysql',
        operatorsAliases: false,
        pool: {
            max: 5,
            min: 0,
            acquire: 30000,
            idle: 10000
        },
        define: {
            timestamps: false
        }
    } );

    const Warehouse = sequelize.define( 'warehouses', {
        warehouse_code: {
            type: Sequelize.STRING,
            primaryKey: true
        },
        warehouse_name: Sequelize.STRING,
    } );

    const Location = sequelize.define( 'locations', {
        location_code: {
            type: Sequelize.STRING,
            primaryKey: true
        },
        location_name: Sequelize.STRING,
        location_warehouse:  {
            type: Sequelize.STRING,
            references: 'warehouses', //table name
            referencesKey: 'warehouse_code'   // column name in the table
        }
    } );

    Warehouse.hasMany(Location , {foreignKey: 'location_warehouse', sourceKey: 'warehouse_code'});
    Location.belongsTo(Warehouse , {foreignKey: 'location_warehouse', targetKey: 'warehouse_code'});

    return {
        models: {
            Location,
            Warehouse,
        }
    }
}

I want Warehouse name in my location table where i have linked location table with warehouse_code.

Upvotes: 2

Views: 8749

Answers (1)

Aayushi Gupta
Aayushi Gupta

Reputation: 367

I got the solution to the problem. All i need to do is

{ include: [ { model: Warehouse, as: 'warehouse' } ] }

While I was giving:

{ raw: true }, { include: { model: Warehouse }}

there was a missing square brackets within include statement

Location.findAll( { include: [ { model: Warehouse, as: 'warehouse' } ] } ).then( locations => {
                if ( locations == null )
                    return res.status( 422 ).send( { header: "Error", content: "err-msg.NoData" } );
                else {
                    Location.count().then( counts => {
                        res.send( {
                            success: true,
                            msg: resp = {
                                locations,
                                counts
                            }
                        } );
                        return;
                    } );
                    return;
                }
            } );

Upvotes: 1

Related Questions