繁体   English   中英

排序联接表而不选择

[英]Sequelize join table without selecting

我想在Sequelize中以多对多关系从联接表中选择ID,并根据端点之一限制结果。 例如:

ModelA:
- id
- deleted_at
- is_expired

ModelB:
- id
- deleted_at
- is_active

ModelAToModelB:
- a_id
- b_id

我想做类似的事情

SELECT id FROM ModelAToModelB ab INNER JOIN ModelB b ON ab.b_id = b.id WHERE ab.id = someA_ID AND b.deleted_at IS NULL;

我目前正在做类似的事情

const ModelB = require("./modelb")
const ModelAToModelB = require("./modelatob");

ModelAToModelB.findAll({
    where: { id: someA_ID },
    include: [
       {
           model: ModelB,
           where: { deleted_at: null }
       }
    ]
})

这似乎可行,除非我还需要从ModelB获取所有数据,而我只需要ModelAToB.id

有什么办法可以废弃ModelB的数据? 或者也许使用ModelA东西来获取关联ID?

const ModelB = require("./modelb")
const ModelAToModelB = require("./modelatob");

ModelAToModelB.findAll({
    where: { id: someA_ID },
    include: [
       {
           model: ModelB.scope(null), //Prevent the default scope from adding attributes
           where: { deleted_at: null },
           required: true, //Force an inner join
           attributes: [] //Join without selecting any attributes
       }
    ]
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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