Created
June 14, 2014 17:33
-
-
Save clouddueling/70cee9b67077bd93b228 to your computer and use it in GitHub Desktop.
Through association with SailsJS. Make sure all references to models are lowercase because that is how they're stored.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
tableName: 'account_roles', | |
attributes: { | |
account_id: 'integer', | |
name: 'string', | |
description: 'text', | |
deleted: 'boolean', | |
users: { | |
collection: 'user', | |
via: 'accountRoles', | |
through: 'accountroleuser' | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
tableName: 'account_role_user', | |
tables: ['users', 'account_roles'], | |
junctionTable: true, | |
attributes: { | |
id: { | |
primaryKey: true, | |
autoIncrement: true, | |
type: 'integer' | |
}, | |
users: { | |
columnName: 'user_id', | |
type: 'integer', | |
foreignKey: true, | |
references: 'user', | |
on: 'id', | |
via: 'accountroles', | |
groupBy: 'user' | |
}, | |
accountRoles: { | |
columnName: 'account_role_id', | |
type: 'integer', | |
foreignKey: true, | |
references: 'accountrole', | |
on: 'id', | |
via: 'users', | |
groupBy: 'accountrole' | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
tableName: 'users', | |
attributes: { | |
name: 'string', | |
password: { | |
type: 'string', | |
minLength: 4, | |
required: true | |
}, | |
bio: 'string', | |
email: { | |
type: 'email', | |
required: true | |
}, | |
cloudinary_public_id: 'string', | |
accountRoles: { | |
collection: 'accountRole', | |
via: 'users', | |
through: 'accountroleuser' | |
} | |
} | |
}; |
Have you experienced any issues with this in which you get duplicate records? (e.g. https://gist.github.com/levity/02052edd445b68174c1e) Thanks for any help you can offer!
Hello!! Following not works, in associated table, no records being added.
User.create(params).populate('roles').exec(function createUser(err, user){
if(err) {
return res.json(err);
}
Role.findOne({name : 'super'}).then(function(role) {
user.roles.add(role.id);
user.save(function(err){
console.log(role.id);
if (err) { return res.serverError(err); }
return res.ok();
});
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this!