【问题标题】:what is the equivalent sequelize query for this raw query?这个原始查询的等效 sequelize 查询是什么?
【发布时间】:2016-04-28 14:39:08
【问题描述】:

SELECT offer.id FROM offer WHERE offer.user_id != '2' and offer.id not in(从 notify_offers where notify_offers.user_id = '2' 中选择 notify_offers.offer_id);

【问题讨论】:

    标签: javascript mysql node.js express sequelize.js


    【解决方案1】:

    如果您已关联 Offers 和 NotifiedOffers 模型,并且它不是多对多关联,您可以这样做:

    Offers.findAll({
        attributes: ['id'],
        where: {
            user_id: {
                not: 2
            }
        },
        include: {
            model: NotifiedOffers,
            where: {
                user_id: {
                    not: 2
                }
            },
            required: true
        }
    });
    

    【讨论】:

    • 如何关联这两个表? offer_id 是 notify_offers 表中的一个字段,作为 offer(id) 的外键引用
    • 我认为NotifiedOffers.belongsTo(Offers,{foreignKey:'offer_id'}) 对你有用,但你可以在这里获得更多信息:docs.sequelizejs.com/en/latest/docs/associations
    猜你喜欢
    • 2013-09-09
    • 2016-12-11
    • 2015-11-30
    • 1970-01-01
    • 2017-05-18
    • 2011-01-05
    • 2014-04-19
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多