首页 文章

Sequelize查询Or-ing where和include语句

提问于
浏览
0

我正在尝试创建一个Sequelize查询,只返回与 where clauseinclude wheres 匹配的记录 . 例如,我有一个属于人模型的用户模型 . 用户模型有一个名为username的字段,person模型具有名字和姓氏 .

示例查询:

{
    "where": {
      "username": {
        "$iLike": "%s%"
      }
    },
    "include": [{
        "model": Person,
        "where": {
            "$or": [{
                "firstName": {
                    "$iLike": "%s%"
                }
            }, {
                "lastName": {
                    "$iLike": "%s%"
                }
            }]
        }
    }]
}

上面的查询匹配具有与“ilike”'s匹配的用户名AND(名字或姓氏)的记录 . 我正在尝试使用用户名OR(名字或姓氏) .

我知道在一个地方工作时如何使用或运营商,但我想使用或在哪里或包括 . 这可能吗?我是否使用了必需的假?

1 回答

  • 2

    试试这个:

    {
    where: {
      $or: [
        {
          userName: {
            $ilike: "%s%"
          }
        },
        {
          '$person.firstName$': {
            $ilike: "%s%"
          }
        },
        {
          '$person.lastName$': {
            $ilike: "%s%"
          }
        }
      ]
    },
    include: [
      {
        model: Person,
      }
    ]
    }
    

相关问题