使用 Sequelize 在父对象中查找子对象

Posted

技术标签:

【中文标题】使用 Sequelize 在父对象中查找子对象【英文标题】:Find Child Object in Parent Object using Sequelize 【发布时间】:2020-06-29 08:30:00 【问题描述】:

我在 mysql 数据库中有三个表:- categoryTypes、categories 和 categoryTemplates。 父表是 categoryTypes。在 categoryTypes 中,有以下字段,categoryTypes 的 id 作为外键存储在 categories 表中。虚拟数据如下:-

"id": 1,
"name": "testing1",
"img": "assets/images/img-1584341288506.png",
"status": "ACTIVE",

在categories表中,有以下字段,其中有一个字段名为categoryTypeRef,其中categoryTypes的主键存储为外键。虚拟数据:-

"id": 1,
"name": "testing1236",
"img": "assets/images/img-1584341317490.png",
"status": "ACTIVE",
"categoryTypeRef": 1,

在categoryTemplates表中,category id作为外键存储在category templates表中,虚拟数据为:-

"id": 2,
"status": "ACTIVE",
"brandName": "Havells",
"purchasingCost": 10000,
"specifications": "\n  \"TextBox\": [\n    \n      \"labelText\": \"Text Box 1\",\n      \"valueText\": \"1234577800\",\n      \"requireTextBox\": false\n    ,\n    \n      \"labelText\": \"Text Box 2\",\n      \"valueText\": \"15264848\",\n      \"requireTextBox\": false\n    \n  ],\n  \"TextArea\": [\n    \n      \"labelTextArea\": \"bdbhd\",\n      \"valueTextArea\": \"hdhdhd\",\n      \"requireTextArea\": false\n    \n  ]\n",
"serialNo": null,
"warrantyMonths": 18,
"categoryRef": 1,

我想在获取类别模板对象时检索类别类型作为不同的对象。 我的预期解决方案是:-

"categoryTemplate": 
            "id": 2,
            "status": "ACTIVE",
            "brandName": "Havells",
            "purchasingCost": 10000,
            "specifications": "\n  \"TextBox\": [\n    \n      \"labelText\": \"Text Box 1\",\n      \"valueText\": \"1234577800\",\n      \"requireTextBox\": false\n    ,\n    \n      \"labelText\": \"Text Box 2\",\n      \"valueText\": \"15264848\",\n      \"requireTextBox\": false\n    \n  ],\n  \"TextArea\": [\n    \n      \"labelTextArea\": \"bdbhd\",\n      \"valueTextArea\": \"hdhdhd\",\n      \"requireTextArea\": false\n    \n  ]\n",
            "serialNo": null,
            "warrantyMonths": 18,
            "categoryRef": 1,
            "category": 
                "id": 1,
                "name": "testing1236",
                "img": "assets/images/img-1584341317490.png",
                "status": "ACTIVE",
                "categoryTypeRef": 1,
            ,
           "categoryType": 
                    "id": 1,
                    "name": "testing1",
                    "img": "assets/images/img-1584341288506.png",
                    "status": "ACTIVE",
                
        

我应用了以下 sequelize 查询:-

 const categoryTemplate = await CategoryTemplate.findAll(
            where: 
              id: body.categoryTemplateRef
            ,
            include: [
              
                model: Category,
                include: [CategoryType]
              
            ]
          );

我的实际结果是:-

"categoryTemplate": 
            "id": 2,
            "status": "ACTIVE",
            "brandName": "Havells",
            "purchasingCost": 10000,
            "specifications": "\n  \"TextBox\": [\n    \n      \"labelText\": \"Text Box 1\",\n      \"valueText\": \"1234577800\",\n      \"requireTextBox\": false\n    ,\n    \n      \"labelText\": \"Text Box 2\",\n      \"valueText\": \"15264848\",\n      \"requireTextBox\": false\n    \n  ],\n  \"TextArea\": [\n    \n      \"labelTextArea\": \"bdbhd\",\n      \"valueTextArea\": \"hdhdhd\",\n      \"requireTextArea\": false\n    \n  ]\n",
            "serialNo": null,
            "warrantyMonths": 18,
            "categoryRef": 1,
            "category": 
                "id": 1,
                "name": "testing1236",
                "img": "assets/images/img-1584341317490.png",
                "status": "ACTIVE",
                "categoryTypeRef": 1,
                "categoryType": 
                    "id": 1,
                    "name": "testing1",
                    "img": "assets/images/img-1584341288506.png",
                    "status": "ACTIVE",
                
            
        

如果有人对此提供帮助,请提前致谢。

【问题讨论】:

【参考方案1】:

categoryTemplate.categoryType = categoryTemplate.category.categoryType

这意味着在您的对象 categoryTemplate 上创建一个名为 categoryType 的新字段,并在其上存储对象 categoryTemplate.category.categoryType

【讨论】:

对不起,我没有得到你。你能描述一下吗?

以上是关于使用 Sequelize 在父对象中查找子对象的主要内容,如果未能解决你的问题,请参考以下文章

PHP在父级中访问子级的私有属性

React Native 对象子映射在父映射中

在 Core Data 中执行子获取时,在父上下文中修改托管对象是不是会向下传播到子上下文?

保存后在子上下文中创建的新对象在父上下文中不存在

Sequelize:急切加载和排序(在父表上)

使用 JPA Hibernate 自动保存子对象