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

153
Vistas
Sequelize use attribute field

I have two models; giftModel and userGiftModel. They are related to each other as follows:

userGiftModel.belongsTo(giftModel, { as: "gift", foreignKey: "giftId"});
giftModel.hasMany(userGiftModel, { as: "userGifts", foreignKey: "giftId"});

I count number of gifts for current user as userGiftCount and my goal is to return a status which is determined by userGiftCount.

Here is my query and the problem is that status always return 1.

    const gifts = await models.giftModel.findAll({
        include: {
            model: models.userGiftModel,
            as: 'userGifts',
            where: {
                userId: req.user.id
            },
            required: false
        },
        where: {
            giftStatus: 1,
            [Op.and]: [
                {
                    [Op.or]: [
                        {
                            giftStartDate : {
                                [Op.lt] : moment().unix()
                            }
                        },
                        {
                            giftStartDate : null
                        }
                    ],
                },
                {
                    [Op.or] : [
                        {
                            giftEndDate : {
                                [Op.gt] : moment().unix()
                            }
                        },
                        {
                            giftEndDate : null
                        }
                    ],
                }
            ]
        },
        attributes: [
            ['giftId', 'id'],
            ['giftName', 'name'],
            ['giftDescription', 'description'],
            ['giftAmount', 'amount'],
            ['giftLink', 'link'],
            ['giftStartDate', 'startDate'],
            ['giftEndDate', 'endDate'],
            ['giftReportCount', 'reportCount'],
            ['giftReportSum', 'reportSum'],
            [fn("COUNT", col("userGifts.giftId")), "userGiftCount"],
            //here is my main problem
            [literal(`CASE WHEN 'userGiftCount' <> 0 THEN 0 ELSE 1 END`), 'status']
        ],
        order: [
            ['giftId', 'desc']
        ],
        group: ['giftId'],
        includeIgnoreAttributes: false,
    })
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