I have a problem when I try to get data on the category table because there is a query error on the relation error log:
SELECT `categories`.`id`,
`categories`.`name`,
`categories`.`color`,
`transactions`.`id` AS `transactions.id`,
`transactions`.`name_transaction` AS `transactions.name_transaction`,
`transactions`.`categoriesId` AS `transactions.categoriesId`,
`transactions`.`amount` AS `transactions.amount`
FROM `categories` AS `categories`
LEFT OUTER JOIN `transactions` AS `transactions`
ON `categories`.`id` = `transactions`.`categoryId`;
error :
Error
at Query.run (E:\project\budget_app\server\node_modules\sequelize\lib\dialects\mysql\query.js:52:25)
at E:\project\budget_app\server\node_modules\sequelize\lib\sequelize.js:311:28
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async MySQLQueryInterface.select (E:\project\budget_app\server\node_modules\sequelize\lib\dialects\abstract\query-interface.js:407:12)
at async Function.findAll (E:\project\budget_app\server\node_modules\sequelize\lib\model.js:1134:21)
at async getAll (E:\project\budget_app\server\app\api\categories\controller.js:5:22)
my table model : - categories
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class categories extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
categories.hasMany(models.transaction)
}
}
categories.init({
name: DataTypes.STRING,
color: DataTypes.STRING
}, {
sequelize,
modelName: 'categories',
});
return categories;
};
-transaction
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class transaction extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
transaction.belongsTo(models.categories)
}
}
transaction.init({
name_transaction: DataTypes.STRING,
categoriesId: DataTypes.INTEGER,
amount: DataTypes.INTEGER
}, {
sequelize,
modelName: 'transaction',
});
return transaction;
};
my categories controller
const { categories, transaction } = require('../../db/models');
module.exports = {
getAll : async(req, res, next) => {
try {
const result = await categories.findAll({
attributes: ['id', 'name', 'color'],
include:{
model: transaction,
attributes: ['id', 'name_transaction', 'categoriesId', 'amount']
}
})
res.status(200).json({
message:"Success",
data: result,
});
} catch (error) {
next(error);
}
},
}
how do i change so that it doesn't error