Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!