简体   繁体   中英

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 ). In the docs it states that, I can use $nested.column$ syntax.

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

Is bookitem a Table? Or a Database?

bookItem.serial either represents db.tbl or tbl.column

bookItem.book.name can only represent db.tbl.column

Since bookItem seems to be a database name, then serial must be a table name. At that point, "tablename LIKE..." is a syntax error.

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.

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

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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