引用单个表的多列 - Sails JS API 模型
Multiple columns referencing single table - Sails JS API Model
最近我开始学习Sails JS,虽然它看起来非常有用(我不必自己构建api?!(,但我目前正在测试sails的当前小项目遇到了一些障碍。
我的主要职业是教师,整个项目的最终目标是列出学生,与他们合作良好的同龄人(friend_id
(,以及他们不合作的学生(unfriend_id
(。利用这些信息,加上他们当前的GPA,我想通过其他一些算法优化座位表。
第一部分,我需要从 Sails 数据服务器返回数据以同意我的意见。
我需要做的是帆(我已经浏览了一对多集合以及多对多和多对一的帆文档,但这个问题似乎很特别(是根据friend_id
或unfriend_id
列为用户收集所有项目。
数据
这个 SqlFiddle 具有基本的模式设置,其中包含一些虚拟数据,供每个人复制/粘贴并在需要时直接使用。
用户
CREATE TABLE `students` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`student_first_name` varchar(200) NOT NULL,
`student_last_name` varchar(255) NOT NULL,
`student_home_phone` varchar(10) DEFAULT NULL,
`student_guardian_email` varchar(255) DEFAULT NULL,
`student_gpa` float NOT NULL DEFAULT '2',
`class_id` tinyint(4) NOT NULL,
UNIQUE KEY `student_id` (`student_id`)
);
关系
CREATE TABLE `relations` (
`relation_id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL DEFAULT '100000',
`friend_id` int(11) DEFAULT NULL,
`unfriend_id` int(11) DEFAULT NULL,
UNIQUE KEY `relation_id` (`relation_id`)
);
虚拟数据(忽略名称(
INSERT INTO `students` VALUES (1,'Paul','Walker','1112223333','fake@email.com',2,1),(2,'Vin','Diesel','1112223333','fake@email.com',3,1),(3,'That','One''Guy','1112223333','fake@email.com',4,1),(4,'Not','Yuagin','1112223333','fake@email.com',2,1),(5,'Hei','Yu','1112223333','fake@email.com',2,1);
INSERT INTO `relations` VALUES (1,1,2,NULL),(2,2,1,NULL),(3,1,NULL,4),(4,4,NULL,1),(5,1,5,NULL),(6,5,1,NULL),(7,2,3,NULL),(8,3,2,NULL);
我尝试了如下方法,但是当我运行它时,api 为两者返回一个空的 json 数组(在我至少会收到学生/关系列表之前,具体取决于我正在寻找的 api(。
学生.js
module.exports = {
connection:'localMysql',
schema: 'true',
attributes: {
student_id: {
type: "integer",
required: true,
unique: true,
primaryKey:true
},
student_first_name:{
type:'string'
},
student_last_name:{
type:'string'
},
student_home_phone:{
type:'integer'
},
student_guardian_email:{
type: 'email'
},
class_id:{
type:'integer'
},
friends:{
collection:'relationship',
via:'student_friends'
}
},
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false
}
关系.js
module.exports = {
connection:'localMysql',
tableName:'relations',
attributes: {
relation_id:{
type: 'integer',
required: true,
unique: true,
primaryKey:true
},
student_id:{
type:'integer'
},
friend_id:{
type:'integer'
},
unfriend_id:{
type:'integer'
},
student_friends:{
collection:'students',
via:'student_id'
}
},
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false
}
我对现在正在发生的所有事情并不完全陌生,但我对 Node 和 Sails 足够陌生,我似乎只是挠头。如果我想要的不能通过集合模型完成,我将在哪里放置代码以使这些事务发生?我假设(但你知道他们说什么......(它会在StudentsController.js
文件中?
博士
我可以获取此 MySQL 查询创建的行为吗:
select
s.*, group_concat(r.friend_id) as friends, group_concat(r.unfriend_id) as unfriends
from
students s
left join
relations r
ON s.student_id = r.student_id
GROUP BY s.student_id;
使用 Sails JS 中的collection
设置进行复制?
如果没有,我应该将代码放在哪里来手动执行此操作?(假设它不是StudentsController.js
(
更新
按照@Solarflare的建议,我设法得到了一些更内联的东西,尽管这是一个非常艰难的开始。现在我得到了一个名为"关系"的列,但里面什么都没有。这比以前我一无所获要好(因为所有返回的东西都是[]
(。但它是一个空数组。(输出如下(。
注意:我从关系模型中删除了student_friends
声明。
我该如何解决这个问题?
[
{
"relationships": [],
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [],
"student_id": 2,
"student_first_name": "Vin",
"student_last_name": "Diesel",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [],
"student_id": 3,
"student_first_name": "That",
"student_last_name": "One'Guy",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [],
"student_id": 4,
"student_first_name": "Not",
"student_last_name": "Yuagin",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [],
"student_id": 5,
"student_first_name": "Hei",
"student_last_name": "Yu",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
}
]
更新 #2
好吧,只是在等待时胡闹,我得到了一些非常基本的工作。我进入Relationship.js
文件并将student_id
的定义更改为model:'students',via:'student_id'
,我至少可以获得给定学生的所有关系。它给了我正确的收藏,但我仍然想知道是否有更直接的方法来摆弄它以获得我需要的东西。以下是现在localhost:1337/students
的输出:
[
{
"relationships": [
{
"relation_id": 1,
"friend_id": 2,
"unfriend_id": null,
"student_id": 1
},
{
"relation_id": 3,
"friend_id": null,
"unfriend_id": 4,
"student_id": 1
},
{
"relation_id": 5,
"friend_id": 5,
"unfriend_id": null,
"student_id": 1
}
],
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [
{
"relation_id": 2,
"friend_id": 1,
"unfriend_id": null,
"student_id": 2
},
{
"relation_id": 7,
"friend_id": 3,
"unfriend_id": null,
"student_id": 2
}
],
"student_id": 2,
"student_first_name": "Vin",
"student_last_name": "Diesel",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [
{
"relation_id": 8,
"friend_id": 2,
"unfriend_id": null,
"student_id": 3
}
],
"student_id": 3,
"student_first_name": "That",
"student_last_name": "One'Guy",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [
{
"relation_id": 4,
"friend_id": null,
"unfriend_id": 1,
"student_id": 4
}
],
"student_id": 4,
"student_first_name": "Not",
"student_last_name": "Yuagin",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"relationships": [
{
"relation_id": 6,
"friend_id": 1,
"unfriend_id": null,
"student_id": 5
}
],
"student_id": 5,
"student_first_name": "Hei",
"student_last_name": "Yu",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
}
]
好的,所以这里的完整内容非常简单,我拿了上面的代码并在这里和那里进行了一些更改,老实说,我不认为它会起作用,但我得到了一些比我以前得到的任何东西都要好的东西。这不是一个完美的解决方案,但就目前而言,它运行良好。
以下是文件,以及我所做的:
学生.js
module.exports = {
connection: 'localMysql',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
student_id: {
type: "integer",
required: true,
unique: true,
primaryKey: true
},
student_first_name: {
type: 'string'
},
student_last_name: {
type: 'string'
},
student_home_phone: {
type: 'integer'
},
student_guardian_email: {
type: 'email'
},
class_id: {
type: 'integer'
},
friends:{
collection:'relationship',
via:'student_id'
},
unfriends:{
collection:'relationship',
via:'student_id'
}
}
};
关系.js
module.exports = {
connection: 'localMysql',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'relations',
attributes: {
relation_id: {
type: 'integer',
required: true,
unique: true,
primaryKey: true
},
student_id: {
model:'students',
via:'student_id'
},
friend_id: {
model:'students',
via:'student_id',
through:'friends'
},
unfriend_id: {
model:'students',
via:'student_id',
through:'unfriends'
}
}
};
然后,我将两个新文件添加到api/models/
目录中,Friends.js
和 Unfriends.js
,它们都非常简单:
朋友.js
module.exports = {
connection: 'localMysql',
schema: false,
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
student_id:{
model:'students',
via:'student_id'
},
friend_id:{
model:'relationship',
via:'friend_id'
}
}
};
解除好友.js
module.exports = {
connection: 'localMysql',
schema: false,
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
student_id:{
model:'students',
via:'student_id'
},
unfriend_id:{
model:'relationship',
via:'unfriend_id'
}
}
};
输出
最重要的事情是"我是否得到了正确的输出",简短的回答是:不,但它是可用的。我还没有得到我原始查询的 Sails 表示,但我已经接近了。
本地主机输出:1337/关系
[
{
"student_id": {
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
"friend_id": {
"student_id": 2,
"student_first_name": "Vin",
"student_last_name": "Diesel",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
"relation_id": 1
},
{
"student_id": {
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
"unfriend_id": {
"student_id": 4,
"student_first_name": "Not",
"student_last_name": "Yuagin",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
"relation_id": 3
},
//Truncated for space
本地主机输出:1337/学生/
[
{
"friends": [
{
"relation_id": 1,
"student_id": 1,
"friend_id": 2,
"unfriend_id": null
},
{
"relation_id": 3,
"student_id": 1,
"friend_id": null,
"unfriend_id": 4
},
{
"relation_id": 5,
"student_id": 1,
"friend_id": 5,
"unfriend_id": null
}
],
"unfriends": [
{
"relation_id": 1,
"student_id": 1,
"friend_id": 2,
"unfriend_id": null
},
{
"relation_id": 3,
"student_id": 1,
"friend_id": null,
"unfriend_id": 4
},
{
"relation_id": 5,
"student_id": 1,
"friend_id": 5,
"unfriend_id": null
}
],
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},//Truncated for length
现在,在StudentsController.js
,我经历了并制定了一种方法,它给了我更多我想要的东西。
学生控制器.js
module.exports = {
get:function(req,res){
Students.find()
.populate('friends',{
'friend_id':{'!':null}
})
.populate('unfriends',{
'unfriend_id':{'!':null}
}).exec(function(err,j){
if(err) res.json(err);
res.json(j);
});
}
};
本地主机的输出:1337/学生/获取(这更接近我想要的只是/学生
[
{
"friends": [
{
"relation_id": 1,
"student_id": 1,
"friend_id": 2,
"unfriend_id": null
},
{
"relation_id": 5,
"student_id": 1,
"friend_id": 5,
"unfriend_id": null
}
],
"unfriends": [
{
"relation_id": 3,
"student_id": 1,
"friend_id": null,
"unfriend_id": 4
}
],
"student_id": 1,
"student_first_name": "Paul",
"student_last_name": "Walker",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
},
{
"friends": [
{
"relation_id": 2,
"student_id": 2,
"friend_id": 1,
"unfriend_id": null
},
{
"relation_id": 7,
"student_id": 2,
"friend_id": 3,
"unfriend_id": null
}
],
"unfriends": [],
"student_id": 2,
"student_first_name": "Vin",
"student_last_name": "Diesel",
"student_home_phone": "1112223333",
"student_guardian_email": "fake@email.com",
"class_id": 1
}//Truncated for length
现在,除了我在控制器中编写的代码之外,为什么任何东西都像现在这样运行,我不知道。但至少它是有效的。
- Sails.js:同时发布文本输入和一个文件
- Sails.js:我可以将一些服务/模型打包为npm并在Sails.jss应用程序中加载npm吗
- 将对象传递到sails.js中的views/layout.ejs
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 引用单个表的多列 - Sails JS API 模型
- 如何使用javascript访问sails.js模型中的属性
- Sails.js/Waterline.add()和.remove()仅适用于第二次调用
- Sails.js指定要从自动插入中排除的js文件
- 聚合Sails.JS/Waterline中的嵌套模型数据
- Sails.js获得多对多关联计数
- 如何在使用Passport.js和Sails.js注册后重定向到特定位置
- 如何防止sails.js在所有视图中传递req对象
- Sails.js在v1、v2等中分组路由
- sails.js中的模拟/存根全局变量
- 在Sails.js中通过关系填充多对多
- 如何将Sails.js挂钩路由连接到策略
- 使用Sails.JS为静态HTML/JS应用程序提供身份验证
- 跳过Sails.js中的模型验证
- Sails.js日志传输依赖于环境
- 如何观察在 Sails .js 中添加到模型中的新记录