SailsJS (0.11.0 also 0.12-rc3): selecting parent data from child id in 1 - M relationship
Parenting with one of Child does not work in Sails JS. Rather, my select statement is giving no result. So looking for the best answer
Model 1: User
module.exports = {
attributes: {
name:{
type:"string",
required:true,
minLength: 3
},
age:{
type:"int",
required:true,
unique: false
},
pets : {
collection : 'pet',
via : 'owners',
dominant: true
}
}
};
Model 2: Pet
module.exports = {
attributes: {
name:{
type:"string",
required:true,
minLength: 2
},
color:{
type:"string",
required:true,
unique: false
},
owners:{
model : 'user'
}
}
};
Data in the system
{
pets: [
{
name: "jimmy",
color: "blue",
createdAt: "2015-07-25T05:39:57.207Z",
updatedAt: "2015-07-25T05:43:06.570Z",
owners: "55b31e06b234f3a6bcab32c6",
id: "55b3212db234f3a6bcab32c9"
}
],
name: "John",
age: "20",
createdAt: "2015-07-25T05:26:30.415Z",
updatedAt: "2015-07-25T05:26:30.415Z",
id: "55b31e06b234f3a6bcab32c6"
}
Thus, the command "Select exits in sails"
A) Finding child (pet) from parent (s) / owner id
sails> Pet.find({}).where({"owners":"55b31e06b234f3a6bcab32c6"}).exec(console.log)
null [ { owners: '55b31e06b234f3a6bcab32c6',
name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
id: '55b3212db234f3a6bcab32c9' } ]
This issue has been resolved at https://github.com/balderdashy/waterline/issues/410
B) Finding the parent (user) from the child id (Pet)
sails> User.find({}).where({"pets":"55b3212db234f3a6bcab32c9"}).exec(console.log)
undefined
sails> User.find({}).where({"pets":"55b3212db234f3a6bcab32c9"}).exec(console.lognull []
sails> User.find({}).where({"pets.id":"55b3212db234f3a6bcab32c9"}).exec(console.log)
undefined
sails> null []
undefined
sails> User.find({}).where({"pets":{"id":"55b3212db234f3a6bcab32c9"}}).exec(console.log)
undefined
sails> null []
sails> User.find({}).where({"pets":{"id":"55b3212db234f3a6bcab32c9"}}).exec(console.log)
Can you please guide me to the above select statement in SailsJS?
Thanks and regards,
Rajah
+3
Raj rajen
source
to share
1 answer
As per the interaction with the SailsJS Team at https://github.com/balderdashy/waterline/issues/1102 , I accept this as a resolution at this time. Hope will improve on other issues listed on Github (waterline)
sails> Pet.find({id: '55b3212db234f3a6bcab32c9'}).populate('owners').exec(console.log)
undefined
sails>
undefined
sails> null [ { owners:
{ name: 'MyUser',
age: '20',
createdAt: '2015-07-25T05:26:30.415Z',
updatedAt: '2015-07-25T05:26:30.415Z',
id: '55b31e06b234f3a6bcab32c6' },
name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
id: '55b3212db234f3a6bcab32c9' } ]
And for the user
User.find({id:'55b31e06b234f3a6bcab32c6'}).populate('pets').exec(console.log)null [ { pets:
[ { name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
owners: '55b31e06b234f3a6bcab32c6',
id: '55b3212db234f3a6bcab32c9' } ],
name: 'MyUser',
age: '20',
createdAt: '2015-07-25T05:26:30.415Z',
updatedAt: '2015-07-25T05:26:30.415Z',
id: '55b31e06b234f3a6bcab32c6' } ]
+2
Raj rajen
source
to share