在 sequelize 中搜索 2 个表

Search on 2 tables in sequelize

Category 上的搜索有效,如何在 Announcement 上也应用搜索?

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

您需要将条件移动到根级别。
像这样:

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