0
I am creating a stock management system where a model has category one for many, doing the insertion correctly, but at the time of listing it returns me the error:
SELECT `luminarias`.`id`, `luminarias`.`estado`, `luminarias`.`modeloL`,
`luminarias`.`qntd`, `luminarias`.`date`, `luminarias`.`createdAt`,
`luminarias`.`updatedAt`, `luminarias`.`modeloId`,
`modelo`.`id` AS `modelo.id`,
`modelo`.`modelo` AS `modelo.modelo`,
`modelo`.`createdAt` AS `modelo.createdAt`,
`modelo`.`updatedAt` AS `modelo.updatedAt`
FROM `luminarias` AS `luminarias`
LEFT OUTER JOIN `modelos` AS `modelo`
ON `luminarias`.`modeloId` = `modelo`.`id`
ORDER BY `luminarias`.`id` DESC;
Conexao estabelecida com sucesso!
(node:20024) UnhandledPromiseRejectionWarning: SequelizeDatabaseError:
Unknown column 'luminarias.modeloId' in 'field list'
at Query.formatError (C:\Users\pedro\Desktop\Tanto Faz\Gerenciamento de
Estoque\node_modules\sequelize\lib\dialects\mysql\query.js:239:16)
at Query.run (C:\Users\pedro\Desktop\Tanto Faz\Gerenciamento de
Estoque\node_modules\sequelize\lib\dialects\mysql\query.js:54:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:20024) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without
a catch block, or by rejecting a promise which was not handled with .catch().
To terminate the node process on unhandled promise rejection, use the CLI
flag `--unhandled-rejections=strict` (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20024) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
My controller:
router.get("/luminaria", (req, res) => {
Luminaria.findAll({
include: [{ model: Modelo }],
order: [
['id', 'DESC']
],
}).then(luminarias => {
res.render("luminaria/index", { luminarias: luminarias });
});
});
My model of Luminaria :
const Sequelize = require("sequelize");
const connection = require("../database/database");
const Modelo = require("../modelos/Modelo");
const Luminaria = connection.define('luminarias', {
estado: {
type: Sequelize.STRING,
allowNull: false
},modeloL: {
type: Sequelize.STRING,
allowNull: false
}, qntd: {
type: Sequelize.INTEGER,
allowNull: false
}, date: {
type: Sequelize.DATE,
allowNull: false
}
});
//Luminaria.sync({force: true});
Modelo.hasMany(Luminaria); // UM modelo tem MUITAS luminarias
Luminaria.belongsTo(Modelo); // UMA luminaria tem UM modelo
module.exports = Luminaria;
Meu model de Modelos:
const Sequelize = require("sequelize");
const connection = require("../database/database");
const Modelo = connection.define('modelos', {
modelo: {
type: Sequelize.STRING,
allowNull: false
}
});
module.exports = Modelo;
I’ve tried to change the type of relationship, but it didn’t work. I believe you’re right, because Insert worked well.
Welcome to [en.so]! To better understand the problem, we need to see how the model of
Luminaria
and also that ofModelo
, you can add this information by clicking on [Edit]– Rafael Tavares