gpt4 book ai didi

mysql - 在风 sails 水线 orm 中填充多个表

转载 作者:行者123 更新时间:2023-11-29 02:51:53 25 4
gpt4 key购买 nike

我正在开发一个包含多个(>2)表的 sails 应用程序,我需要在填充方法的帮助下加入这些表例如

Category.js 模型

attributes: {
CategoryID:{
type:"integer",
required:true,
primaryKey:true,
autoIncrement:true
},
SubCategories:{ //REFERING TO SUB-CATEGORY TABLE
collection:'SubCategory',
via:'CategoryID'
},
CategoryName:{
type:"string",
required:true,
maxLength:50
}
}

这是SubCategory.js 模型

attributes: {
id:{
type:'integer',
required:true,
primaryKey:true,
autoIncrement:true,
maxLength:10,
columnName:'SubCategoryID'
},
CategoryID:{
model:'Category' //REFERING TO CATEGORY TABLE
},
ProductsOfCategory:{ //REFERING TO PRODUCT TABLE
collection:'Product',
via:'SubCategoryID'
},
SubCategory:{
type:"string",
required:true,
maxLength:50
}
}

Product.js 模型

attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
maxLength: 10,
columnName:'ProductID'
},
SubCategoryID: {
model:'SubCategory'
},
ProductDetails:{
collection:'ProductDetails',
via:'ProductID'
},
ProductName: {
type: "string",
required: true,
maxLength: 50
}
}

ProductDeatils.js 模型

attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
maxLength: 10,
columnName:'ProductDetailID'
},
ProductID:{
model:'Product'
},
Size:{
type:"string",
required:true,
maxLength:10
},
Color:{
type:"string",
required:true,
maxLength:10
}
}

在填充时,我能够填充每个类别的类别和子类别。

Category.find()
.populate('SubCategories')
.exec(function(err, categories){
if (err) {
console.log(err);
return res.json(err);
}
console.log(categories);
res.json(categories);
})

如何一次性填充上述所有表格,以便在最终查询后我们在一个 json 中获得上述所有详细信息。

我们得到以上所有表的连接

这是一个包含所有子类别的类别,包含所有产品的子类别以及所有产品在一个 json 中包含产品详细信息

最佳答案

你问了一个很好的问题。人们对将嵌套填充功能放入 sails 产生了大量兴趣,实际上有数十个问题请求和 PR 等。

在这里看看一些历史:

[FEATURE REQUEST] Recursively populate #308 - 我迟到了,在 2014 年 10 月 29 日提出请求,您将在历史中看到这一点。

据我所知,大多数对话最终都汇聚于此(经过几年的 Sails 用户请求该功能):

Deep populate #1052 (该问题在 2016 年 1 月 14 日 撰写时仍未解决)

从我们所处的那个问题的状态来看还不清楚。这两个链接的历史确实表明其他人使用过的替代解决方法。

我的直觉是不支持开箱即用的递归填充。

我在使用 SailsJS 水线模型关联时所做的是使用像 async.js 这样的包。 - 使用瀑布之类的东西以编程方式显式填充子关系。您可以将此操作与覆盖默认值 toJSON() 结合起来您调用以将它们的关系(您以编程方式填充)添加到 JSON 响应的模型。您同样可以选择使用内置的 promise 来实现相同的目的。

找到这个(日期,2014 年)SOF Question其中提供了更多信息。

有人,如果我在最近的 Sails 或 Waterline 版本中错过了这个功能添加,请在这里纠正我 - 在这两个项目的发行说明中找不到任何说明支持此功能的内容。

关于mysql - 在风 sails 水线 orm 中填充多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770293/

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