gpt4 book ai didi

sails.js - SailsJS(0.11.0 和 0.12-rc3): Selecting Parent Data from Child ID in 1 - M relationship

转载 作者:行者123 更新时间:2023-12-04 03:36:46 25 4
gpt4 key购买 nike

选择父项和其中一个子项在 Sails JS 中不起作用。相反,我的选择语句没有产生结果。所以,寻找最佳答案

模型 1:用户

  module.exports = {

attributes: {

name:{
type:"string",
required:true,
minLength: 3
},
age:{
type:"int",
required:true,
unique: false
},
pets : {
collection : 'pet',
via : 'owners',
dominant: true
}
}
};

模型 2:宠物

         module.exports = {

attributes: {
name:{
type:"string",
required:true,
minLength: 2
},
color:{
type:"string",
required:true,
unique: false
},
owners:{
model : 'user'
}
}
};

系统中的数据

            {
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"
}

因此,sails 控制台中的 Select 语句是

A) 从 parent (用户)/所有者 ID 中找到 child (宠物)

    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' } ]

此问题已在 https://github.com/balderdashy/waterline/issues/410 中解决

B)从 child ID(宠物)找到 parent (用户)

    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)

能否指导我使用 SailsJS 中的上述 select 语句?

感谢和问候,

拉吉

最佳答案

根据与 SailsJS 团队在 https://github.com/balderdashy/waterline/issues/1102 上的互动,我现在接受这个作为解决方案。希望事情会根据 Github 中列出的其他问题得到改善(水线)

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' } ]

对于用户

 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' } ]

关于sails.js - SailsJS(0.11.0 和 0.12-rc3): Selecting Parent Data from Child ID in 1 - M relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31624487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com