Sequelize和2个连接表,麻烦

时间:2014-12-03 10:30:23

标签: postgresql sequelize.js

我们遇到了问题。我们不能使用sequelize在下面的结构上执行SQL SELECT。

这是我们的结构,见下图。

enter image description here

我们正在尝试执行此续集查询:

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"",因为我们正在尝试在alert.keywords之外选择属性include[],但它仍然在以前的DID中工作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

1 个答案:

答案 0 :(得分:0)

在include上包含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']
    }
]

为了查询products.sent_deal,你不应该包括SentDeal吗?对于sequelize GH的问题,这个问题可能是更好的套件