有很多通过关系,计数总是返回0

时间:2016-03-16 19:53:06

标签: loopbackjs

我有一个公司表,一个地址表和一个公司地址表来规范多对多的关系。 Loopback工作正常,可以添加公司,为公司添加地址,甚至可以使用API​​资源管理器使用(POST / Companies / {id} / addresses / {fk})方法查询与公司关联的地址。

然而,当我尝试获取与公司相关的所有地址(GET / Companies / {id} / addresses)时,我得到一个空数组。此外,当我计算一个特定公司有多少地址(GET / Companies / {id} / addresses / count)时,我总是得到0.

我确定我错过了一些非常小的东西。我的数据源是postgresql。

/* ----- common/models/companies.json ----- */

{
  "name": "Companies",
  "base": "User",
  "strict": true,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
      "companyName": {
      "type": "string",
      "required": true
   },
     "firstName": {
     "type": "string",
     "required": true
   },
    "lastName": {
      "type": "string",
      "required": true
   },
    "cellNumber": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "addresses": {
      "type": "hasMany",
      "model": "Addresses",
      "foreignKey": "addressesid",
      "through": "CompaniesAddresses"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "create"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }
  ],
  "methods": {}

/* ---- common/models/addresses.json ---- */

{
  "name": "Addresses",
  "base": "PersistedModel",
  "strict": true,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "streetaddress2": {
      "type": "string"
    },
    "phonenumber2": {
      "type": "string"
    },
    "phonenumber1": {
      "type": "string",
      "required": true
    },
    "zippostalcode": {
      "type": "string",
      "required": true
    },
    "stateprov": {
      "type": "string",
      "required": true
    },
    "streetaddress1": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
     "companies": {
       "type": "hasMany",
       "model": "Companies",
       "foreignKey": "companiesid",
       "through": "CompaniesAddresses"
    }
  },
  "acls": [
      {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}

/* ---- companiesaddresses.json ---- */
{
  "name": "CompaniesAddresses",
  "base": "PersistedModel",
  "strict": true,
  "idInjection": false,
  "options": {
    "validateUpsert": true
   },
  "properties": {
    "companiesid": {
      "type": "number",
      "id": true,
      "required": true
    },
    "addressesid": {
      "type": "number",
      "id": true,
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "addresses": {
      "type": "belongsTo",
      "model": "Addresses",
      "foreignKey": "addressesid"
    },
    "companies": {
      "type": "belongsTo",
      "model": "Companies",
      "foreignKey": "companiesid"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

/* ---- server/boot/model-config.json ---- */

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "Companies": {
    "dataSource": "pg",
    "public": true,
    "$promise": {},
    "$resolved": true
  },
  "Addresses": {
    "dataSource": "pg",
    "public": true,
    "$promise": {},
    "$resolved": true
  },
  "CompaniesAddresses": {
    "dataSource": "pg",
    "public": true,
    "$promise": {},
    "$resolved": true
  }
}

1 个答案:

答案 0 :(得分:0)

公司和地址中的外键如下:

**common/models/companies.json** "relations": { "addresses": { "type": "hasMany", "model": "Addresses", "foreignKey": "addressesid", <---- this should be companiesid "through": "CompaniesAddresses" } } 在另一方面(地址)做同样的修改,一切都会起作用。