简体   繁体   English

双多对多关系

[英]Double many-to-many relationship

In loopback I have defined two models with a double many-to-many relationship as follows: 在环回中,我定义了两个具有一对多关系的模型,如下所示:

action.json: 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: 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": {}
    }

Now when I try to create a relation between an action model and a course model with the following PUT request: 现在,当我尝试使用以下PUT请求在动作模型和课程模型之间创建关系时:

http://localhost:3000/api/courses/57331a4eeff440cb02c886ae/benchmark/rel/5731d60da2c6d238e3c3d9b3 http:// localhost:3000 / api / courses / 57331a4eeff440cb02c886ae / benchmark / rel / 5731d60da2c6d238e3c3d9b3

Then when I request the course model with the related action models included with the following GET request: 然后,当我请求课程模型以及以下GET请求中包含的相关动作模型时:

http://localhost:3000/api/courses/57331a4eeff440cb02c886ae?filter=%7B%22include%22%3A%5B%22benchmark%22%2C%22main%22%5D%7D http:// localhost:3000 / api / courses / 57331a4eeff440cb02c886ae?filter =%7B%22include%22%3A%5B%22benchmark%22%2C%22main%22%5D%7D

I get: 我得到:

    {
      "name": "Introduction Lessons",
      "id": "57331a4eeff440cb02c886ae",
      "benchmark": [{
        "text": "text here",
        "id": "5731d60da2c6d238e3c3d9b3"
      }],
      "main": [{
        "text": "text here",
        "id": "5731d60da2c6d238e3c3d9b3"
      }]
    }

So apparently the action is now attached through both the benchmark as the main relation. 因此,显然现在通过基准作为主要关系附加了动作。 How did this happen? 这怎么发生的? Do I setup my models wrong? 我的模型设置是否错误?

From what I understand, when you use hasAndBelongsToMany relashionship, loopback uses a through table which it manages automatically for you. 据我了解,当您使用hasAndBelongsToMany冲突时,环回会使用一个穿透表,该表会自动为您管理。

I believe by default it calls itself From_modelTo_model. 我相信默认情况下它自称为From_modelTo_model。 In order to have two such relashion you need to tell loopback to manage it differently otherwise they use the same through table. 为了产生两个这样的冲突,您需要告诉回送以不同的方式进行管理,否则它们将使用相同的穿透表。

Try with through option such as 尝试通过选项,例如

Action 行动

"benchmark": {
  "type": "hasAndBelongsToMany",
  "model": "Course",
  "foreignKey": "benchmark",
  "through":"ActionCourseBenchmark"
},
"main": {
  "type": "hasAndBelongsToMany",
  "model": "Course",
  "foreignKey": "main",
  "through":"ActionCourseMain"
}

Course 课程

"benchmark": {
  "type": "hasAndBelongsToMany",
  "model": "Action",
  "foreignKey": "benchmark",
  "through":"ActionCourseBenchmark"
},
"main": {
  "type": "hasAndBelongsToMany",
  "model": "Action",
  "foreignKey": "main",
  "through":"ActionCourseMain"
}

see this github issue for more details 请参阅此github问题以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM