我创建了一个包含两个模型 Person 和 Department 的 Sails 应用程序。 他们有一对一的关系。Sails-mysql 是使用的适配器。当我尝试使用 where() 或 sort() 标准填充部门详细信息以及 Person 时,结果记录未排序或 where() 是不适用。
Person.js
attributes: {
firstname:{
alpha:true,
required:true
},
lastname:{
alpha:true,
required:true
},
age:{
numeric:true,
required:true
},
department:{
model:'Department'
}
}
Department.js
attributes: {
DepartmentName:{
required:true,
alpha:true
},
Description:{
required:true,
alpha:true
},
//relation
person:{
model:'Person'
}
PersonController.js
Person.find().populate('department').where({DepartmentName:{"startsWith":"hr"}}).sort('Description desc').exec(console.log);
不工作。
我以所有可能的方式尝试了 where() 和 sort()
var sort='DepartmentName desc';
并在 populate() 中传递该变量,如下所示:
Person.find().populate('department',sort).exec(console.log);
但这也不起作用。以同样的方式我尝试了 where() 也是失败的。
帮帮我。
最佳答案
对我来说这是有效的 ;)
Person.find().populate('department', {
where: {
DepartmentName: {
'startsWith': 'hr'
}
},
sort: 'DepartmentName desc'}
).exec(......);
有关更多信息,请查看此处:http://sailsjs.org/documentation/reference/waterline-orm/queries/populate 在 Populating a collection association
部分下关于sorting - where() 和 sort() 不适用于 sails 中填充的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096600/