Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

node.js - Sequelize Error : missing FROM-clause entry for table

I am trying to write sequelize query as shown below. I am getting an error missing FROM-clause entry for table "PIs->Item". When i logged the db queries, i found the issue.It is calling another SELECT query inside the FROM clause which is leading to error. How can i solve this issue?

const filter = {
  where: {},
  include: [{
    model: db.PurchasedItems,
    as: 'PIs',
    include: [{
      model: db.Item,
    }],
}],    

filter.where = {
    [Op.or]: [{
      name: {
        [Op.iLike]: `%${options.searchQuery}%`,
      },
    }, {
      '$PIs.Item.name$': {
        [Op.iLike]: `%${options.searchQuery}%`,
      },
    }],
  };

DB log

SELECT /*All columns from main table and lef joined table*/
FROM(SELECT /*columns of main table only*/
     FROM /*main table*/
     WHERE "MainTable"."name" ILIKE '%w%' OR "PIs->Item"."name" ILIKE '%w%'
) AS MainTable
LEFT OUTER JOIN "PurchasedItems" AS "PIs" ON "MainTable"."id" = "PIs"."purchaseId"
LEFT OUTER JOIN "Items" AS "PIs->Item" ON "PIs"."itemId" = "PIs->Item"."id"
question from:https://stackoverflow.com/questions/65932230/sequelize-error-missing-from-clause-entry-for-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...