Muthukumar Marichamy
Muthukumar Marichamy

Reputation: 1214

Sailsjs through relation request format

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

AS per above link, I have created 3 models "User", "Pet" and "Pet_user", then I used below request to insert the record. But, it inserts user and pet table instead of pet_user table.

{
    "name":"BB21",
    "user_name":"BB21",
    "email_personal" : "[email protected]",
    "email_official" : "[email protected]",    
    "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}); 
        } 
    });
},

Upvotes: 0

Views: 44

Answers (1)

Aravind Kumar
Aravind Kumar

Reputation: 98

Id has to be included while using through in model collection

{
"name":"BB21",  
"user_name":"BB21",
"email_personal" : "[email protected]",
"email_official" : "[email protected]",    
    "pets" : [
        {
            "id" : 1
        }   
    ]
}

Upvotes: 1

Related Questions