2014-12-03 12 views
0

我們遇到了問題。我們無法使用sequelize在以下結構中執行SQL SELECT。續集和2連接表,麻煩

這是我們的結構,見下圖。

enter image description here

我們正在努力執行該sequelize查詢:

query = { 
    'attributes': [models.sequelize.fn('count', models.sequelize.col('sent_deals.user_id'))], 


    order: [['alert.keywords', ' DESC']], 
    attributes: ['alert.keywords'], 
    include: [ 
     { 
      model: models.alert, 
      attributes: ['keywords'] 
     }, { 
      where: { 
       'products.sent_deal.created_at': { 
        between: [moment(start).startOf('day').format('YYYY-MM-DD HH:mm'), moment(end).endOf('day').format('YYYY-MM-DD HH:mm')] 
       } 
      }, 
      model: models.product, 
      attributes: ['id'] 
     } 
    ] 
}; 



models.userAlerts.findAll(query) 

但後來我們收到以下錯誤:"error: missing FROM-clause entry for table "alert"",因爲我們試圖選擇還外include[]屬性alert.keywords,但是它仍然在Sequelize JS的早期版本中工作!現在我們不能再通過警報來命令了...... :(它總是返回一個警報,因爲「屬於」連接,所以理論上它必須工作。

我的猜測是因爲查詢沒有執行直接加入,但在一個子查詢做了SELECT FROM,見下圖:

SELECT "userAlerts".* 
    ,"alert"."id" AS "alert.id" 
    ,"alert"."keywords" AS "alert.keywords" 
    ,"products"."id" AS "products.id" 
    ,"products.sent_deal"."user_alert_id" AS "products.sent_deal.user_alert_id" 
    ,"products.sent_deal"."deal_id" AS "products.sent_deal.deal_id" 
    ,"products.sent_deal"."created_at" AS "products.sent_deal.created_at" 
FROM (
    SELECT "userAlerts"."id" 
     ,"userAlerts"."user_id" 
     ,"userAlerts"."alert_id" 
     ,"userAlerts"."activationToken" 
     ,"userAlerts"."activatedAt" 
     ,"userAlerts"."createdAt" 
     ,"userAlerts"."updatedAt" 
    FROM "user_alerts" AS "userAlerts" 
    WHERE (
      SELECT "products.sent_deal"."user_alert_id" 
      FROM "sent_deals" AS "products.sent_deal" 
      INNER JOIN "deals" AS "products" ON "products"."id" = "products.sent_deal"."deal_id" 
      WHERE "userAlerts"."id" = "products.sent_deal"."user_alert_id" 
       AND (
        "products.sent_deal"."created_at" BETWEEN '2014-11-17 00:00' 
         AND '2014-12-02 23:59' 
        ) LIMIT 1 
      ) IS NOT NULL 
    ORDER BY "alert"."keywords" DESC LIMIT 1 
    ) AS "userAlerts" 
LEFT JOIN "alerts" AS "alert" ON "alert"."id" = "userAlerts"."alert_id" 
INNER JOIN (
    "sent_deals" AS "products.sent_deal" INNER JOIN "deals" AS "products" ON "products"."id" = "products.sent_deal"."deal_id" 
    ) ON "userAlerts"."id" = "products.sent_deal"."user_alert_id" 
    AND (
     "products.sent_deal"."created_at" BETWEEN '2014-11-17 00:00' 
      AND '2014-12-02 23:59' 
     ) 
ORDER BY "alert"."keywords" DESC 

回答

0

擁有對包括keywords屬性,應該是足夠了,沒有必要將它添加到外部查詢

attributes: [models.sequelize.fn('count', models.sequelize.col('sent_deals.user_id'))], 


order: [['alert.keywords', ' DESC']], 
attributes: ['alert.keywords'], <-- This line is superflous 
include: [ 
    { 
     model: models.alert, 
     attributes: ['keywords'] 
    }, { 
     where: { 
      'products.sent_deal.created_at': { 
       between: [moment(start).startOf('day').format('YYYY-MM-DD HH:mm'), moment(end).endOf('day').format('YYYY-MM-DD HH:mm')] 
      } 
     }, 
     model: models.product, 
     attributes: ['id'] 
    } 
] 

您是否應該包含SentDeal以查詢產品ucts.sent_deal?這個問題可能是更好的問題上的續集GH

+0

因爲:...現在我們不能通過警報命令了... 這就是爲什麼我們添加(當我們使用以前版本的sequelize:1.7。 8)'''attributes:['alert.keywords']''',所以可以在對象的頂層(包含結果)而不在主對象的「alert」對象內顯示的警報。所以我們可以根據警報排序所有結果。你明白我的意思嗎?但是''''''''''''''''''線路不能工作,因爲我們試圖升級到2.0.0。而sent_deals現在是一個連接表,我們用它在產品和user_alerts模型中使用「through」連接到它。 – 2014-12-03 12:23:01

+0

小例子,我們想要的東西是:''{{alert:'test2'},{alert:'test1'}}'''而不是''{{alert:{keywords:'test2'}}, {alert:{keywords:'test1'}}}'''在1.7.8這個工作,與「superflous」線,但在2.0.0不再,似乎... – 2014-12-03 12:31:28