【发布时间】: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",
}
}
}
如果有人对此提供帮助,请提前致谢。
【问题讨论】:
标签: mysql node.js database sequelize.js