Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
Sequelize findAll, EJS For iteration to show which sub categories correspond to a category in a optgroup and a few selects

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
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda