【问题标题】:Double many-to-many relationship双多对多关系
【发布时间】: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 请求中包含的相关操作模型请求课程模型时:

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

我明白了:

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

因此,显然现在通过基准作为主要关系附加了该操作。这怎么发生的?我的模型设置有误吗?

【问题讨论】:

  • 你的本地主机将如何作为我的本地主机工作???!?!
  • @user2181397 不会的,我没有在线演示。
  • 您能详细说明一下,您想通过这种双重关系实现什么目标?为什么一个还不够?我也对你的外键感到困惑。你能解释一下,你为什么要这样设置它们吗?
  • 我在动作模型的 foreignKeys 中有错字,我在编辑中更正了。
  • 一门课程有几个基准动作和一些主要动作。所有动作都可以作为基准或主要动作分配给课程。这就是为什么我认为我需要与同一模型建立双重关系。

标签: javascript loopbackjs strongloop


【解决方案1】:

据我了解,当您使用 hasAndBelongsToMany relashionship 时,环回使用它自动为您管理的直通表。

我相信默认情况下它称自己为 From_modelTo_model。为了有两个这样的关系,你需要告诉 loopback 以不同的方式管理它,否则他们使用相同的直通表。

尝试使用通过选项,例如

动作

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

课程

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

查看this github issue了解更多详情

【讨论】:

  • 感谢您的回答!关于它的一些注意事项:您可以将相同的模型用于 Action.benchmark 作为 Course.benchmark(命名为 CourseActionBenchmark)。 Action.main 和 Course.main(命名为 CourseActionMain)也是如此。您还需要手动创建直通模型。
猜你喜欢
  • 2017-11-11
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2019-08-04
  • 2012-11-17
  • 1970-01-01
相关资源
最近更新 更多