繁体   English   中英

双多对多关系

[英]Double many-to-many relationship

在环回中,我定义了两个具有一对多关系的模型,如下所示:

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"
      }]
    }

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

据我了解,当您使用hasAndBelongsToMany冲突时,环回会使用一个穿透表,该表会自动为您管理。

我相信默认情况下它自称为From_modelTo_model。 为了产生两个这样的冲突,您需要告诉回送以不同的方式进行管理,否则它们将使用相同的穿透表。

尝试通过选项,例如

行动

"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"
}

请参阅此github问题以获取更多详细信息

暂无
暂无

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

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