How to get join data result without prefix table name in Sequelize ORM

こ雲淡風輕ζ 提交于 2019-12-05 00:49:23

This should work,

db.admin.findAll({ 
    attributes: ['id', 'username', 'email', 'status', 'role.role_id', 'role.role_name'],
    include: [{ 
      model: db.role,                      
      where:{status : 'Active'}, 
      attributes: []
    }],
    raw: true
})

I found this here

The top level model also can be modified to exclude and include the properties if the query still want to use along with

raw: true

 db.admin.findAll({ 
    include: [{ 
      model: db.role,                      
      where:{status : 'Active'},
      attributes: ["role_id", "role_name"],  
      nested: false,  
    }],
    attributes: {exclude: [],
                 include: ["role.role_id", "role.role_name"]},
    raw: true      
  });

additionally you can have aliases as well

 db.admin.findAll({ 
    include: [{ 
      model: db.role,                      
      where:{status : 'Active'},
      attributes: [["role_id", "roleId"], ["role_name", "roleName"]] 
    }],
    attributes: {exclude: [],
                 include: ["role.roleId", "role.roleName"]},
    raw: true      
  });

raw: true , causes the issue while returning data with associations , solution to this is :

Remove :

raw: true 

From :

db.admin.findAll({ 
    include: [{ 
      model: db.role,                      
      where:{status : 'Active'},     
    }],
    raw: true // <--------- Remove this   
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!