Orm Sails.js+;水线:通过关联多对多
我不熟悉Sails.js(v0.10.5)和水线ORM。我在数据库中有3个表:用户(id、名称)、角色(id、别名)和联接表用户\角色(用户\ id、角色\ id)。不要更改数据库中的表名和字段名,这一点很重要。我希望策略实体是用户和角色之间的连接实体。以下是一些映射代码:Orm Sails.js+;水线:通过关联多对多,orm,many-to-many,sails.js,waterline,Orm,Many To Many,Sails.js,Waterline,我不熟悉Sails.js(v0.10.5)和水线ORM。我在数据库中有3个表:用户(id、名称)、角色(id、别名)和联接表用户\角色(用户\ id、角色\ id)。不要更改数据库中的表名和字段名,这一点很重要。我希望策略实体是用户和角色之间的连接实体。以下是一些映射代码: //User.js module.exports = { tableName: 'users', autoCreatedAt: false, autoUpdatedAt: false, att
//User.js
module.exports = {
tableName: 'users',
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'integer',
required: true
},
name: {
type: 'string'
},
roles: {
collection: 'role',
via: 'users',
through: 'policy'
},
}
}
//Role.js
module.exports = {
tableName: "roles",
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'integer',
required: true
},
alias: {
type: 'string',
required: true
},
users: {
collection: 'user',
via: 'roles',
through: 'policy'
}
}
}
//Policy.js
module.exports = {
tableName: "users_roles",
tables: ['users', 'roles'],
junctionTable: true,
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
user: {
columnName: 'user',
type: 'integer',
foreignKey: true,
references: 'user',
on: 'id',
via: 'role',
groupBy: 'user'
},
roles: {
columnName: 'role',
type: 'integer',
foreignKey: true,
references: 'role',
on: 'id',
via: 'user',
groupBy: 'role'
}
}
}
但当我试图访问控制器中的角色时
User.findOne({id: 1}).populate('roles').exec(function(err, user) {
console.log(JSON.stringify(user.roles));
});
这是回报
[]
及
返回
{"id":1,"name":"test", "roles":[]}
我两次检查数据库中是否存在用户、角色和它们之间的关联。我犯了什么错 我找到了解决这个问题的方法。这不是我想要的,但它确实有效。 第一:加入实体:
//Policy.js
module.exports = {
tableName: "users_roles",
autoPK: false,
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
},
user: {
columnName: 'user_id',
model: 'user'
},
role: {
columnName: 'role_id',
model: 'role'
}
},
//tricky method to get all users for specified role_id
//or to get all roles for specified user_id
get: function(id, modificator, cb) {
var fields = ['user', 'role'];
if (fields.indexOf(modificator) < 0) {
cb(new Error('No such modificator in Policy.get()'), null);
}
var inversedField = fields[(fields.indexOf(modificator) + 1) % 2];
var condition = {};
condition[inversedField] = id;
this.find(condition).populate(modificator).exec(function(err, policies) {
if (err) {
cb(err, null);
return;
}
var result = [];
policies.forEach(function(policy) {
result.push(policy[modificator]);
});
cb(null, result);
return;
});
}
}
和角色实体:
//Role.js
module.exports = {
tableName: 'roles',
autoPK: false,
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
},
alias: {
type: 'string',
required: true,
unique: true,
},
policies: {
collection: 'policy',
via: 'role'
}
}
}
这就是我获取指定用户id的所有角色的方式:
...
id = req.session.me.id; //user_id here
Policy.get(id, 'role', function(err, roles) {
var isAdmin = false;
roles.forEach(function(role) {
isAdmin |= (role.id === 1);
});
if (isAdmin) {
next(null);
return;
} else {
return res.redirect('/login');
}
});
...
也许它对某人有用=)你能把代码传过去吗?确保您正在执行User.find().populate('roles').exec(function(){})@mandeep_m91谢谢您的回复!我编辑了问题(console.log输出中有一些错误)。您能解决这个问题吗?如果没有,您正在使用哪个底层数据库?@mandeep_m91我已经解决了这个问题。我向策略实体添加了一个ID字段,并将用户和策略(以及角色和策略)之间的关联更改为OneToMany。这不是我真正想要的,但它很有效。@mandeep_m91我正在使用postgresql 9.3几乎两年后它对我有用!谢谢
//Role.js
module.exports = {
tableName: 'roles',
autoPK: false,
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
},
alias: {
type: 'string',
required: true,
unique: true,
},
policies: {
collection: 'policy',
via: 'role'
}
}
}
...
id = req.session.me.id; //user_id here
Policy.get(id, 'role', function(err, roles) {
var isAdmin = false;
roles.forEach(function(role) {
isAdmin |= (role.id === 1);
});
if (isAdmin) {
next(null);
return;
} else {
return res.redirect('/login');
}
});
...