我很难找到如何与Bookshelf.js和postgres建立深厚的关系。
假设我有三种型号,User,Org和UserRole…User和Org有着多到多的关系。UserRole和User通过Org是多对多的.我在概念上挣扎的地方是如何完成:User 1属于Org A和Org B;User 1应该是Org A中的Admin角色,而不是Org B中的Admin角色。
我还没有在bookshelf.js文档或互联网络中找到任何示例或提示,文档总是指以单数而不是表格的形式连接表。如果能朝正确的方向推进,我们将不胜感激。
发布于 2015-04-14 14:52:25
鉴于上述情况,两国关系如下:
belongsToMany "Org“hasMany”UserRole用户belongsToMany用户”hasMany "UserRole”belongsTo "Org”belongsTo”用户“下面是如何通过“Org”为“用户”加载"UserRoles“:
UserRole.forge({
name: 'Admin',
user_id: 1,
org_id: 2
})
.save()
.then(function () {
User.forge({ id: 1 }).fetch().then(function (user) {
user.user_roles().query({ where: { org_id: 2 }}).fetch().then(function (user_roles) {
console.log('user_roles: %j', user_roles);
}),
});
});https://stackoverflow.com/questions/29609699
复制相似问题