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

154
Visualizações
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 à 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