sas js很多很多协会没有工作

时间:2017-12-24 08:46:33

标签: sails.js

user.js的

module.exports = {
  tableName: 'user',

  attributes: {
    firstName: {
      type: 'string',
    },
    lastName: {
      type: 'string',
    },
    id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    pets: {                     //integer
     collection:'pet',
     via:'owners'
    }
  }
};

================================

Pet.js
------
module.exports = {
  tableName: 'pet',

  attributes: {
    id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    breed: {
      type: 'string',
    },
    type: {
      type: 'string',
    },
    name: {
      type: 'string',
    },
    owners: {                    //integer 
      collection:'user',
      via:'pets'
    }
  }
};

但是当我点击蓝图api http://localhost:1337/userhttp://localhost:1337/pet时,我没有得到相关记录

以下是ARC或邮递员输出的用户和宠物

[
  {
"pets": [],
"firstName": "a",
"lastName": "aa",
"id": 1
},
  {
"pets": [],
"firstName": "b",
"lastName": "bb",
"id": 2
},
  {
"pets": [],
"firstName": "c",
"lastName": "cc",
"id": 3
},
  {
"pets": [],
"firstName": "d",
"lastName": "dd",
"id": 4
},
  {
"pets": [],
"firstName": "e",
"lastName": "ee",
"id": 5
}
],


[
  {
"owners": [],
"id": 1,
"breed": "dalmatian",
"type": "male",
"name": "tom"
},
  {
"owners": [],
"id": 2,
"breed": "labrador",
"type": "female",
"name": "kg"
},
  {
"owners": [],
"id": 3,
"breed": "doberman",
"type": "male",
"name": "tq"
},
  {
"owners": [],
"id": 4,
"breed": "puppy",
"type": "male",
"name": "yu"
}
]

一对多的协会工作正常,但不知道我在很多人中错过了什么? 我尝试从两个模型中删除id列,但没有帮助

1 个答案:

答案 0 :(得分:0)

尝试使用add Blueprint API

向宠物添加宠物
POST /:model/:id/:association/:fk

示例:

http://localhost:1337/user/1/pets/2

这会将id为2的宠物添加到ID为1的所有者。