Sailsjs通过关系请求格式

时间:2016-05-16 17:46:36

标签: sails.js

http://sailsjs.org/documentation/concepts/models-and-orm/associations/through-associations

根据以上链接,我创建了3个模型" User"," Pet"和" Pet_user",然后我使用下面的请求来插入记录。但是,它会插入user和pet表而不是pet_user表。

{
    "name":"BB21",
    "user_name":"BB21",
    "email_personal" : "BB21@yopmail.com",
    "email_official" : "BB21@gmail.com",    
    "pets" : [
        {
            "pet" : 1,
            "owner" : 1

        }   
    ]
}

//Controller     
create: function(req, res) { 
    User.create(req.params.all()).exec(function(err, user) {
        if (err) 
            return res.json(402, err);
        if(user) {  
            res.json('200', {message: sails.config.messages.USERCREATION}); 
        } 
    });
},

1 个答案:

答案 0 :(得分:1)

Id has to be included while using through in model collection

{
"name":"BB21",  
"user_name":"BB21",
"email_personal" : "BB21@yopmail.com",
"email_official" : "BB21@gmail.com",    
    "pets" : [
        {
            "id" : 1
        }   
    ]
}