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

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -