Reputation: 201
Selection of Parent with one of the Child isn't working in Sails JS. Rather, my select statement's aren't yielding result. So, looking for 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 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"
}
So, the Select Statements in sails console are
A) Finding the Child (Pet) from Parent (Users) / 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 is resolved in 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 in the above select statement in SailsJS?
Thanks and Regards,
Raj
Upvotes: 3
Views: 587
Reputation: 201
As per the interaction with SailsJS Team on https://github.com/balderdashy/waterline/issues/1102, I am accepting this as resolution at this time. Hope things will improve as per other issues listed in 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 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' } ]
Upvotes: 2