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

185
Vistas
How to get Associated model row count in Sequelize?

Hi I have a following code. From this I need a total rows count from the Comment Model. Can you please help? Thanks in Advance!

Post.hasMany(models.Comment, {
  as: 'postComment',
  foreignKey: 'postId',
});

Comment.belongsTo(models.Post, {
  foreignKey: 'postId',
  as: 'post',
});

        return Post.findAndCountAll({
            attributes: [['postId', 'id'], 'target_population', 'title', ['type', 'category'], 'stage', ['text_content', 'comment'], ['time_posted', 'engagement_timeframe'], ['closing_date', 'engagement_completion'], ['engagement_end', 'engagement_close'], ['non_constituent_view', 'non_constituents'], ['discussion_board_enabled', 'discussion_board'], 'attached_documents', 'description'],
            where: where,
            offset: offset,
            limit: limit,
            distinct: true,
            include: [
                {
                    model: Survey,
                    as: 'survey',
                    attributes: ['isOpen'],
                    where: {isOpen: "TRUE"},
                    required: false,
                    include: [
                        {
                            model: Question,
                            as: 'question',
                            include: [
                                {
                                    model: ProposedAnswer,
                                    as: 'choice',
                                    attributes: [['proposedAnswerId', 'id'], ['answer', 'value']],
                                    required: false,
                                }
                            ],
                            attributes: [['questionId', 'id'], ['question', 'title'], ['questionType', 'type']]
                        }
                    ]
                },
                {
                    model: User,
                    as: 'user',
                    attributes: ['first_name'],
                    where: {userId: {[Op.not]: null}},
                },
                {
                    model: Topic,
                    as: 'topic',
                    attributes: [['topicId', 'id'], ['name', 'value']],
                    through: {attributes: []},
                },
                {
                    model: Comment,
                    as: 'postComment',
                },
            ],
            order: [
                ['postId', 'DESC'],
            ]
        })
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