简体   繁体   English

Sequelize 包括嵌套列:“where 子句”中的未知列

[英]Sequelize include nested column: Unknown column in 'where clause'

I am trying to use the Sequelize ORM's feature that allows referring the nested column from the included Models (See Sequelize Docs: Complex where clauses at the top-level ).我正在尝试使用 Sequelize ORM 的功能,该功能允许从包含的模型中引用嵌套列(请参阅Sequelize Docs: Complex where clauses at the top-level )。 In the docs it states that, I can use $nested.column$ syntax.在文档中指出,我可以使用$nested.column$语法。

The following is what I was trying to do:以下是我正在尝试做的事情:

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);

However, for the code above, I am getting the following error:但是,对于上面的代码,我收到以下错误:

"Unknown column 'bookItem.serial' in 'where clause'"

What am I missing?我错过了什么?

Full DB Schema: https://dbdiagram.io/d/5e08b6aaedf08a25543f79cb完整的数据库架构: https://dbdiagram.io/d/5e08b6aaedf08a25543f79cb

Is bookitem a Table? bookitem是一个表吗? Or a Database?还是数据库?

bookItem.serial either represents db.tbl or tbl.column bookItem.serial代表db.tbltbl.column

bookItem.book.name can only represent db.tbl.column bookItem.book.name只能代表db.tbl.column

Since bookItem seems to be a database name, then serial must be a table name.既然bookItem好像是数据库名,那么serial肯定是表名。 At that point, "tablename LIKE..." is a syntax error.此时,“tablename LIKE...”是一个语法错误。

In your linked documentation books has no name column, change $bookItem.book.name$ to $bookItem.book.title$ , and try adding right: true below required: false to create an inner join.在您的链接文档中 books has no name column,将$bookItem.book.name$更改为$bookItem.book.title$ ,然后尝试在required: false下面添加right: true以创建内部联接。

I have corrected this error on my side.我已经在我这边纠正了这个错误。 Initially, I am writing this query最初,我正在写这个查询

but now I have rearranged the query and it works但现在我重新排列了查询并且它有效

WRONG QUERY错误查询

 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,
  });

FINAL WORKING QUERY最终工作查询

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

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM