Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
Is there any way in Sequelize to search on a table with data in another table which is a foreign key to that table?

My product table

const Product = sequelize.define('products', {
    id: {
        type: Sequelize.STRING(12),
        allowNull: false,
        primaryKey: true,
    },
    name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
    },
    brand: {
        type: Sequelize.STRING(40),
        allowNull: false,
    },
    instock: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false,
    },
    mrp: {
        type: Sequelize.DECIMAL,
        defaultValue: 0.0,
        allowNull: false,
    },
    price: {
        type: Sequelize.DECIMAL,
        defaultValue: 0.0,
    },
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE,
});

Product.belongsTo(Category, {
    foreignKey: 'category_id',
    as: 'categorys',
});

Here the category is a foreign key. I want to search with category name. Do I need to fetch the category first and use the id to search for products?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You definitely can search by fields in an associated model, just use where option inside include:

const products = await Products.findAll({
  include: [{
    model: Category,
    as: 'categorys',
    required: true,
    where: {
      name: categoryName
    }
  }]
})

If you already have a category id then better search products by it, that way you won't need to use include option at all.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda