node.js - N:M eager loading (include) using sequelizejs -


so found myself troubles trying use include eager loading feature in m:n relations

the user model relation:

user.belongstomany(models.rol, {       through: {         model: models['usuario_rol']       },       as: {         plural: 'roles',         singular: 'rol'       },       foreignkey: 'idusuario' }); 

the rol model relation:

rol.belongstomany(models.usuario, {       through: {         model: models['usuario_rol']       },       foreignkey: 'idrol' }); 

finally query:

db.usuario.findone({     where: {       id: insert.id     },     include: [       {         model: db.rol       }     ] }); 

if try crashes error: rol not associated usuario!

the curious instance of sequelize user object can fetch roles using user.getroles()

do have idea of why happening?

when specify alias in association function (in case roles) have pass include well:

return user.findone({   where: {     id: 52   },   include: [     {       model: rol,       as: 'roles'     }   ] }); 

you can save association , pass instead:

user.roles = user.belongstomany(models.rol, {   through: {     model: models['usuario_rol']   },   as: {     plural: 'roles',     singular: 'rol'   },   foreignkey: 'idusuario' });  return user.findone({   where: {     id: 52   },   include: [     {       association: user.roles     }   ] }); 

Comments

Popular posts from this blog

javascript - How to process users in one specific order using map o each function? -

java - Two interface have same method name with different return type -

javascript - Linking from page A to a specific iframe on page B -