簡體   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