回送hasMany通过给出空数组

时间:2018-06-22 09:57:14

标签: loopbackjs loopback

我有以下定义的模型:

学院

{
  "name": "academy",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "required": true
    },
    "name": {
      "type": "string",
      "required": true
    },
    "created": {
      "type": "date"
    },
    "updated": {
      "type": "date"
    },
    "academy_version_id": {
      "type": "number",
      "required": true
    },
    "description": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "versions": {
      "type": "hasMany",
      "model": "academy_version",
      "foreignKey": "academy_id",
      "through": "academy_has_versions"
    }
  },
  "acls": [],
  "methods": {}
}

academy_version

        {
      "name": "academy_version",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "id": {
          "type": "number",
          "required": true
        },
        "name": {
          "type": "string",
          "required": true
        },
        "created": {
          "type": "date"
        },
        "updated": {
          "type": "date"
        }
      },
      "validations": [],
      "relations": {
        "academies": {
          "type": "hasMany",
          "model": "academy",
          "foreignKey": "academy_version_id",
          "through": "academy_has_versions"
        }
      },
      "acls": [],
      "methods": {}
    }

academy_has_versions

{
  "name": "academy_has_versions",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "required": true
    },
    "academy_id": {
      "type": "number",
      "required": true
    },
    "academy_version_id": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {
    "academy": {
      "type": "belongsTo",
      "model": "academy",
      "foreignKey": "academy_id"
    },
    "academy_version": {
      "type": "belongsTo",
      "model": "academy_version",
      "foreignKey": "academy_version_id"
    }
  },
  "acls": [],
  "methods": {}
}

现在您可以看到我的academy模型具有hasManyacademy_has_versions

当我尝试请求此请求时,我得到以下结果:

enter image description here

如您所见,我得到一个空数组(版本)

这是不正确的,因为我的数据库如下所示:

academy_has_versions

enter image description here

academy_version

enter image description here

那么有人能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

我找不到错误,但建议您尝试以下操作:

  1. 由于idInjection设置为true,因此从所有模型中删除“ id”属性。
  2. 删除与外键对应的属性。
  3. 从学院模型中删除academy_version_id属性,因为该关系没有意义。

学院

{
  "name": "academy",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "created": {
      "type": "date"
    },
    "updated": {
      "type": "date"
    },
    "description": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "versions": {
      "type": "hasMany",
      "model": "academy_version",
      "foreignKey": "academy_id",
      "through": "academy_has_versions"
    }
  },
  "acls": [],
  "methods": {}
}

academy_version

{
      "name": "academy_version",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "name": {
          "type": "string",
          "required": true
        },
        "created": {
          "type": "date"
        },
        "updated": {
          "type": "date"
        }
      },
      "validations": [],
      "relations": {
        "academies": {
          "type": "hasMany",
          "model": "academy",
          "foreignKey": "academy_version_id",
          "through": "academy_has_versions"
        }
      },
      "acls": [],
      "methods": {}
    }

academy_has_versions

{
  "name": "academy_has_versions",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "academy": {
      "type": "belongsTo",
      "model": "academy",
      "foreignKey": "academy_id"
    },
    "academy_version": {
      "type": "belongsTo",
      "model": "academy_version",
      "foreignKey": "academy_version_id"
    }
  },
  "acls": [],
  "methods": {}
}