Help with association methods

314 views
Skip to first unread message

Joshua Chan

unread,
May 11, 2015, 9:54:28 AM5/11/15
to sequ...@googlegroups.com
I have the following models defined.
var User = sequelize.define('user', {
    name
: Sequelize.STRING,
    dateOfBirth
: Sequelize.DATEONLY,
    email
: Sequelize.STRING,
    phone
: Sequelize.CHAR(10)
}, {
    timestamps
: false,
    freezeTableName
: true // Model tableName will be the same as the model name
});


var Group = sequelize.define('group', {
    name
: Sequelize.STRING
},{
    timestamps
: false,
    freezeTableName
: true
})




var Role = sequelize.define('role', {
    name
: Sequelize.STRING
},{
    timestamps
: false,
    freezeTableName
: true
})


var UserXGroupXRole = sequelize.define('userXgroupXrole', {
    userId
: {
        type
: Sequelize.INTEGER,
        references
: "user",
        referencesKey
: "id",
        allowNull
: false
   
},
    groupId
: {
        type
: Sequelize.INTEGER,
        references
: "group",
        referencesKey
: "id",
        allowNull
: false
   
},
    roleId
: {
        type
: Sequelize.INTEGER,
        references
: "role",
        referencesKey
: "id",
        allowNull
: false
   
}
},{
    timestamps
: false,
    freezeTableName
: true
})


User.hasMany(UserXGroupXRole)
Group.hasMany(UserXGroupXRole)
Role.hasMany(UserXGroupXRole)
UserXGroupXRole.hasOne(User, { foreignKey: 'id' })
UserXGroupXRole.hasOne(Group, { foreignKey: 'id' })
UserXGroupXRole.hasOne(Role, { foreignKey: 'id' })

Now, I'm trying to create a new user with some records in the userXgroupXrole table like so
function bootstrapUser(profile){
   
var user = User.build();
    user
.name = profile.name;
    user
.email = profile.email;


   
return DefaultMembership.findAll({where: {email: profile.email}})
       
.then(function(memberships){


            memberships
.forEach(function(membership){
               
var ugr = UserXGroupXRole.build();
                ugr
.group = membership.getGroup();
                ugr
.role = membership.getRole();
                user
.addUserXGroupXRole(ugr);
           
})


           
return;
       
})
       
.then(function(){
           
return user.save()
       
})
}

But I'm getting an exception at user.addUserXGroupXRole. Do I have to use an alias to get the methods for adding associated records?

Mick Hansen

unread,
May 11, 2015, 10:29:56 AM5/11/15
to Joshua Chan, sequ...@googlegroups.com
Whats the exception? That the method does not exist?
Try logging Object.keys(ser.Instance.prototype) to see what methods it has.
--
Mick Hansen
@mhansendev
mhansen.io

Joshua Chan

unread,
May 11, 2015, 11:53:29 AM5/11/15
to sequ...@googlegroups.com, joshua.be...@gmail.com
The exception is
Unhandled rejection TypeError: user.addUserXGroupXRole is not a function
    
When I try to log Object.keys(user.Instance.prototype), I get an error 
Unhandled rejection TypeError: Cannot read property 'prototype' of undefined


Mick Hansen

unread,
May 11, 2015, 1:59:23 PM5/11/15
to Joshua Chan, sequ...@googlegroups.com
It has to be Model.Instance.prototype, not instance.Instance.prototype

Joshua Chan

unread,
May 11, 2015, 2:16:25 PM5/11/15
to sequ...@googlegroups.com, joshua.be...@gmail.com
Ah! The method is addUserXgroupXroles. Can I change the casing?


Joshua Chan

unread,
May 11, 2015, 2:17:44 PM5/11/15
to sequ...@googlegroups.com
Actually, I'd really like this to read addGroupRole(). Is tha possible?

Mick Hansen

unread,
May 11, 2015, 2:30:58 PM5/11/15
to Joshua Chan, sequ...@googlegroups.com
Try {as: 'groupRole'} on the association.

On Mon, May 11, 2015 at 8:17 PM, Joshua Chan <joshua.be...@gmail.com> wrote:
Actually, I'd really like this to read addGroupRole(). Is tha possible?



Joshua Chan

unread,
May 11, 2015, 2:57:14 PM5/11/15
to sequ...@googlegroups.com, joshua.be...@gmail.com
Ok, I had some problems getting aliases to work. I think I read something about needing to include the alias when eager loading. Is that right?

Mick Hansen

unread,
May 12, 2015, 4:18:06 AM5/12/15
to Joshua Chan, sequ...@googlegroups.com
Correct, if the association has an alias you'll need to use include: [{model: Model, as: 'as'}].
However you can also simply save the reference to the association like User.GroupRoles = User.hasMany(Role, {as: 'asdsa'}) and then use include: [User.GroupRoles)

On Mon, May 11, 2015 at 8:57 PM, Joshua Chan <joshua.be...@gmail.com> wrote:
Ok, I had some problems getting aliases to work. I think I read something about needing to include the alias when eager loading. Is that right?



Reply all
Reply to author
Forward
0 new messages