Home > Software design >  Search on 2 tables in sequelize
Search on 2 tables in sequelize

Time:10-07

The search on Category is working, how to apply search on Announcement as well?

const announcementList = await Announcement.findAll({
    include: [
    {
        model: Category,
        where: {
            [Op.or]: [
                {
                    name: {
                    [Op.like]: '%'   keyword   '%' 
                    }
                },
                {
                    'Announcement.description': {
                    [Op.like]:'%'   keyword   '%' 
                 }
                }
            ]
        }
    },
   ]
})

CodePudding user response:

You need to move conditions to the root level.
Something like this:

const announcementList = await Announcement.findAll({
  where: {
    [Op.or]: [
    {
      '$Category.name$': {
        [Op.like]: '%'   keyword   '%' 
      }
    },
    {
      'description': {
        [Op.like]:'%'   keyword   '%' 
      }
    }
    ]
  },
  include: [
    {
        model: Category,
    },
   ]
})
  • Related