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