Tur*_*yes 3 javascript orm sequelize.js
我想从 Sequelize 中多对多关系的连接表中选择 ID,并根据端点之一限制结果。例如:
ModelA:
- id
- deleted_at
- is_expired
ModelB:
- id
- deleted_at
- is_active
ModelAToModelB:
- a_id
- b_id
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情
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;
Run Code Online (Sandbox Code Playgroud)
我目前正在做类似的事情
const ModelB = require("./modelb")
const ModelAToModelB = require("./modelatob");
ModelAToModelB.findAll({
where: { id: someA_ID },
include: [
{
model: ModelB,
where: { deleted_at: null }
}
]
})
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但除此之外ModelB,当我想要的只是ModelAToB.id.
有什么办法可以报废ModelB数据吗?或者可能使用 fromModelA来获取关联 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
}
]
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1364 次 |
| 最近记录: |