【发布时间】:2016-09-07 19:36:47
【问题描述】:
在环回中,我定义了两个具有双重多对多关系的模型,如下所示:
action.json:
{
"name": "Action",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"text": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "benchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "main"
}
},
"acls": [],
"methods": {}
}
course.json:
{
"name": "Course",
"base": "TrashModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "benchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "main"
}
},
"acls": [],
"methods": {}
}
现在,当我尝试使用以下 PUT 请求在动作模型和课程模型之间创建关系时:
http://localhost:3000/api/courses/57331a4eeff440cb02c886ae/benchmark/rel/5731d60da2c6d238e3c3d9b3
然后,当我使用以下 GET 请求中包含的相关操作模型请求课程模型时:
我明白了:
{
"name": "Introduction Lessons",
"id": "57331a4eeff440cb02c886ae",
"benchmark": [{
"text": "text here",
"id": "5731d60da2c6d238e3c3d9b3"
}],
"main": [{
"text": "text here",
"id": "5731d60da2c6d238e3c3d9b3"
}]
}
因此,显然现在通过基准作为主要关系附加了该操作。这怎么发生的?我的模型设置有误吗?
【问题讨论】:
-
你的本地主机将如何作为我的本地主机工作???!?!
-
@user2181397 不会的,我没有在线演示。
-
您能详细说明一下,您想通过这种双重关系实现什么目标?为什么一个还不够?我也对你的外键感到困惑。你能解释一下,你为什么要这样设置它们吗?
-
我在动作模型的 foreignKeys 中有错字,我在编辑中更正了。
-
一门课程有几个基准动作和一些主要动作。所有动作都可以作为基准或主要动作分配给课程。这就是为什么我认为我需要与同一模型建立双重关系。
标签: javascript loopbackjs strongloop