postgresql - Sequelize 查询 where on children
问题描述
我有一个用户和国家模型。用户属于许多国家,反之亦然。如何查询countries
数组中只有特定国家/地区 ID 的用户?
const users = await DB.User.findAll({
where: {
// What to put here?
},
include: [
{
model: DB.Country,
as: 'countries',
// Putting where here only causes the countries to filter
},
],
});
解决方案
由于您希望任何用户具有特定的国家/地区,因此无需在用户的 where 子句中放置任何内容,请将 where 子句放在国家/地区包含部分中。
const users = await DB.User.findAll({
where: {
// Nothing to put here
},
include: [
{
model: DB.Country,
as: 'countries',
where:{
id : YOUR_EXPECTED_COUNTRY_ID
}
},
],
});
推荐阅读
- html - 如何在go html模板中根据分隔符拆分字符串
- python - 组合多个具有相同列名但索引不同的 Pandas 系列
- javascript - 如何知道 compositionComplete 何时完成?
- kubernetes - K8 / Cert-Manager / 错误和可能的 YAML 错误配置
- java - 从 PreparedStatement 中的批次获取参数值
- php - Woocommerce - 自动将特定产品添加到新客户的已完成订单中
- oauth-2.0 - 这是否可以使用 cognito 的授权代码授权类型作为 api-gateway 中的授权人?
- json - 为什么 jq 打印记录冗余且不匹配以及如何解决?
- python - Tensorflow 安装错误:__ 不是此平台上支持的轮子
- vue.js - 我无法在 VueJs 中捕获发出的事件