'Issue when creating an item with sequelize

I am having the following issue when trying to create an item using sequelize:

Executing (default): INSERT INTO productos (id_productos,idcolor,producto,idmarca,precio,descripcion,idtalle,idcategoria_producto,cantidad_stock) VALUES (DEFAULT,?,?,?,?,?,?,?,?); (node:14492) UnhandledPromiseRejectionWarning: Error at Query.run (C:\Users\DAMIAN\Desktop\Node\Luna\node_modules\sequelize\lib\dialects\mysql\query.js:46:25) at C:\Users\DAMIAN\Desktop\Node\Luna\node_modules\sequelize\lib\sequelize.js:626:28 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async MySQLQueryInterface.insert (C:\Users\DAMIAN\Desktop\Node\Luna\node_modules\sequelize\lib\dialects\abstract\query-interface.js:749:21)

I understand that the error is because I did not add the catch in the function, but I want to know why the function is failing.

Here is the controller:

create: (req, res) => {

    db.Producto.create({
        producto: req.body.name_product,
        idcategoria_producto: req.body.category_product,
        idcolor: req.body.color_product,
        idmarca: req.body.brand_product,
        precio: req.body.price_product,
        descripcion: req.body.descript_product,
        idtalle: req.body.size_product,
        cantidad_stock: req.body.quantity_product
    })
    
    res.redirect("/")
}

The association of the product I am trying to create:

module.exports = function(sequelize, dataTypes){
    let alias = "Producto";
    
    let cols = {
        id_productos: {
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        idcolor: {
            type: dataTypes.INTEGER
        },
        producto: {
            type: dataTypes.STRING(45)
        },
        idmarca: {
            type: dataTypes.INTEGER
        },
        precio: {
            type: dataTypes.INTEGER
        },
        descripcion: {
            type: dataTypes.STRING(255)
        },
        idtalle: {
            type: dataTypes.INTEGER
        },
        idcategoria_producto: {
            type: dataTypes.INTEGER
        },
        cantidad_stock: {
            type: dataTypes.INTEGER
        }
    }
    let config = {
        tableName: "productos",
        timestamps: false
    }
    let Producto = sequelize.define(alias, cols, config);

    Producto.associate = function(models){

        Producto.belongsTo(models.Marca, {
            as: "marca",
            foreignKey:"idmarca"
        })

        Producto.belongsTo(models.Color, {
            as: "color",
            foreignKey:"idcolor"
        })

        Producto.belongsTo(models.Categoria, {
            as: "categoria",
            foreignKey:"idcategoria_producto"
        })

        Producto.belongsTo(models.Talle, {
            as: "talles",
            foreignKey:"idtalle"
        })

        Producto.hasMany(models.Carrito, {
            as: "producto-carrito",
            foreignKey: "idproducto"
        })

        Producto.hasMany(models.Facturas, {
            as: "producto-facturas",
            foreignKey: "id_producto_facturas"
        })}

    return Producto
}

And the associations of the foreing keys:

module.exports = function(sequelize, dataTypes){
    let alias = "Talle";
    let cols = {
        id_talles: {
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        talles: {
            type: dataTypes.STRING(10)
        },
    }
    let config = {
        tableName: "talles",
        timestamps: false
    }
    let Talle = sequelize.define(alias, cols, config);

    Talle.associate = function(models){
        Talle.hasMany(models.Producto, {
            as: "productos-talle",
            foreignKey: "idtalle"
        })}
        
    return Talle
}

---------------------------------------------------------------------

module.exports = function(sequelize, dataTypes){
    let alias = "Categoria";
    let cols = {
        id_categoria_producto: {
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        categoria_producto: {
            type: dataTypes.STRING(20)
        },
        temporada: {
            type: dataTypes.STRING(20)
        }
    }
    let config = {
        tableName: "categoria_producto",
        timestamps: false
    }
    let Categoria = sequelize.define(alias, cols, config);

    Categoria.associate = function(models){
        Categoria.hasMany(models.Producto, {
            as: "productos-categoria",
            foreignKey: "idcategoria_producto"
        })}
        
    return Categoria
}

-------------------------------------------------------------------------

module.exports = function(sequelize, dataTypes){
    let alias = "Color";
    let cols = {
        id_colores: {
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        nombre_color: {
            type: dataTypes.STRING(20)
        }
    }
    let config = {
        tableName: "colores_productos",
        timestamps: false
    }
    let Color = sequelize.define(alias, cols, config);

    Color.associate = function(models){
        Color.hasMany(models.Producto, {
            as: "productos-color",
            foreignKey: "idcolor"
        })}

    return Color
}

----------------------------------------------------------------

module.exports = function(sequelize, dataTypes){
    let alias = "Marca";
    let cols = {
        id_marcas: {
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        marcas: {
            type: dataTypes.STRING(30)
        },
    }
    let config = {
        tableName: "marcas",
        timestamps: false
    }
    let Marca = sequelize.define(alias, cols, config);

    Marca.associate = function(models){
        Marca.hasMany(models.Producto, {
            as: "productos-marcas",
            foreignKey: "idmarca"
        })}
        
    return Marca
}

Probably is a silly mistake, but I don´t find it. Thanks in advance.



Solution 1:[1]

I had similar issue and i resolved it by editing my

db.sequelize.sync()

to

db.sequelize.sync().then((e)=>{console.log(e) }).catch((e )=>{ console.log(e) });

REASON db.sync() is an asyncronous method and so needs to be handle asynchronously using .then() and .catch()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eclat_soft