mysql 序列化包含嵌套列:“where子句”中的列未知

bq3bfh9z  于 2023-01-20  发布在  Mysql
关注(0)|答案(3)|浏览(138)

我正在尝试使用Sequelize ORM的特性,该特性允许从包含的模型中引用嵌套列(参见Sequelize Docs: Complex where clauses at the top-level)。在文档中,它声明,我可以使用$nested.column$语法。
以下是我尝试做的事情:

let where = { memberId };
if (req.query.search) {
  const like = { [Op.like]: `%${req.query.search}%` };
  where = {
    ...where,
    [Op.or]: [
      { '$bookItem.serial$': like },
      { '$bookItem.book.name$': like },
      { '$bookItem.book.ISBNCode$': like },
    ],
  };
}

const options = {
  where,
  include: [
    {
      model: models.BookItem,
      as: 'bookItem',
      required: false,
      include: [
        {
          model: models.Book,
          as: 'book',
          attributes,
          required: false,
        },
      ],
    },
  ],
});

const transactions = await models.Borrow.findAll(options);

但是,对于上面的代码,我得到了以下错误:
"Unknown column 'bookItem.serial' in 'where clause'"
我错过了什么?
完整数据库架构:https://dbdiagram.io/d/5e08b6aaedf08a25543f79cb

eqzww0vc

eqzww0vc1#

bookitem是表还是数据库?
bookItem.serial代表db.tbltbl.column
bookItem.book.name只能表示db.tbl.column
由于bookItem似乎是数据库名,因此serial必须是表名。此时,“tablename LIKE ...”是语法错误。

vshtjzan

vshtjzan2#

在您的链接文档书籍中没有名称列,请将$bookItem.book.name$更改为$bookItem.book.title$,并尝试在required: false下面添加right: true以创建内部联接。

r3i60tvu

r3i60tvu3#

我已经更正了这个错误。最初,我编写这个查询
但现在我 * 重新排列了查询,它工作 *

查询错误

let where = {
    [op.and]: [
        { id: partner_id },
        { [op.or]: [
          { '$customers.customer_name$': { [op.like]: '%' + query + '%'} },
          { '$customers.email$': { [op.like]: '%' + query + '%'} },
        ]},
    ],
};
  // const where = {
  //   id: partner_id
  // }
  return await this.deliveryBoys.findOne({

    attributes: [
      ['id', 'partner_id'],
      'delivery_boy_name',
      'email',
      'profile_picture',
      'phone_number',
      'fcm_token',
    ],
    include: [
      {
        model: this.customers,
        as: 'customers',
        attributes: ['id', 'customer_name', 'email', 'profile_picture', 'phone_number'],
        require: true,
        where: {
          customer_name: {
            [op.like]: '%' + query + '%'
          }
        },
        include: [
          {
            model: this.company,
            as: 'company',
          },
          {
            model: this.address,
            as: 'address',
            required: false,
            where: {
              status: 1
            }
          }
        ]
      },

    ],
    where,
  });

最终工作查询

let where = {
      [op.and]: [
          { '$deliveryBoys.id$': partner_id },
          { [op.or]: [
            { '$customers.email$': { [op.like]: '%' + query + '%'} },
            { '$customers.customer_name$': { [op.like]: '%' + query + '%'} },
            { '$customers.phone_number$': { [op.like]: '%' + query + '%'} },
            { '$company.name$': { [op.like]: '%' + query + '%'} },
          ]},
       ],
     };

   return await this.customers.findAll({
      attributes: ['id', 'customer_name', 'email', 'profile_picture', 'phone_number'],
      include:[
        {
          model: this.deliveryBoys,
          as: 'deliveryBoys',
          attributes: ['id','delivery_boy_name','phone_number','email','profile_picture','status',],
          where:{
            id: partner_id
          }
        },
        {
          model: this.company,
          as: 'company',
        },
        {
          model: this.address,
          as: 'address',
          required: false,
          where: {
            status: 1
          }
        }
      ],
      where

    });

相关问题