Ankita
Ankita

Reputation: 3

How Can I write INNER Join query using Sequelize on Node.js

I have two tables. One is tblEmailAuditLog having columns Id, OrderId, Body, CommunicationType, CreatedOn, Reciever, WorkflowStatusId, IsSuccess another one is tblLookUp having column names as LU_LookUpValue and LU_LOOKUPID. I want to join the two tables based on the columns (CommunicationType and LU_LOOKUPID)

My inner join query looks as below:

select Id, OrderId, Body, CommunicationType, CreatedOn, Reciever, WorkflowStatusId, IsSuccess, LU_LookUpValue from tblEmailAuditLog INNER JOIN tblLookUp on tblEmailAuditLog.CommunicationType = tblLookUp.LU_LOOKUPID;

I am trying to write the above query using sequelize ORM. I have tried the below things.

1.


    let auditLogs = await MediMarket.TblEmailAuditLog.findAll({
              include: [
                {
                  model: MediMarket.TblLookUp,
                  required: true,
                  where: { orderId: req.body.orderId },
                },
              ],
            });
    



2.
let auditLogs = MediMarket.TblEmailAuditLog.hasMany(
        MediMarket.TblLookUp,
        { foreignKey: "CommunicationType" }
      );

      MediMarket.TblLookUp.belongsTo(MediMarket.TblEmailAuditLog, {
        foreignKey: "LU_LOOKUPID",
      });

      MediMarket.TblEmailAuditLog.findAll({
        where: { orderId: req.body.orderId },
        include: [MediMarket.TblLookUp],
      });

But it is not giving the expected result.

Upvotes: 0

Views: 322

Answers (0)

Related Questions