I want to display in a few OPTGROUPS and a couple of OPTIONS with a for iteration, a category in each optgroup and the sub categories that are related to each category (the relationship was created in mysql with a fk for category_id in the subcategory table), and later the model in my project with sequelize
1
SUB CATEGORIES MODEL id: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true }, nombre: { type: dataTypes.STRING(50) }, fk_categoria: { type: dataTypes.INTEGER }
CATEGORIES MODEL
let cols = {
id: {
type: dataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
nombre: {
type: dataTypes.STRING(50)
}
};
CATEGORIES ASSOCIATION WITH SUB CATEGORIES
Categoria.associate = function(models){
Categoria.hasMany(models.SubCategoria,{
as: 'sub_categoria',
foreignKey: 'fk_categoria'
})
Categoria.hasMany(models.Producto,{
as: 'producto',
foreignKey: 'fk_categoria'
})
}
SUB CATEGORIES ASSOCIATION WITH CATEGORIES
SubCategoria.associate = function(models){
SubCategoria.belongsTo(models.Categoria,{
as: 'categoria',
foreignKey: 'fk_categoria'
})
SubCategoria.hasMany(models.Producto,{
as: 'productos',
foreignKey: 'fk_sub_categoria'
})
}
AND THE PROMISES I WAS USING TO ASK FOR THE DATA, WITHOUT RELATION
let promiseCategoria = db.Categoria.findAll();
let promiseSubCategoria = db.SubCategoria.findAll();
THE DATA TABLE
[1]: https://i.stack.imgur.com/Mpzkw.png
Sorry for the huge post but I needed to show everything to make you understand