I have three tables in MySQL database:- categoryTypes, categories and categoryTemplates. The parent table is categoryTypes. In categoryTypes, there are the following fields and id of categoryTypes is stored as a foreign key in categories table. The dummy data as following:-
"id": 1,
"name": "testing1",
"img": "assets/images/img-1584341288506.png",
"status": "ACTIVE",
In categories table, there are the following fields and there is a field named as categoryTypeRef where primary key of categoryTypes is stored as foreign key. The dummy data:-
"id": 1,
"name": "testing1236",
"img": "assets/images/img-1584341317490.png",
"status": "ACTIVE",
"categoryTypeRef": 1,
In categoryTemplates table, category id is stored as foreign key in category templates tableand the dummy data is:-
"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,
I want to retrieve category type as different object while getting category templates object. My expected solution is:-
"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",
}
}
I applied following sequelize query:-
const categoryTemplate = await CategoryTemplate.findAll({
where: {
id: body.categoryTemplateRef
},
include: [
{
model: Category,
include: [CategoryType]
}
]
});
My actual result is:-
"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",
}
}
}
Thanks in advance if anyone help with this.