【发布时间】:2019-12-17 20:11:24
【问题描述】:
我有 3 张桌子
Post: post_id, name
TermRelationship: post_id, term_taxonomy_id
TermTaxonomy: term_taxonomy_id, name
关系:
Post 到 TermRelationship 是一对多
TermRelationship 到 TermTaxonomy 是一对一
我正在找到一个包含所有数据的帖子,一切都很好:
Post.findOne({
where: { post_id: 351043 },
include: [
{
model: TermRelationships,
include: [{ model: TermTaxonomy }]
}
]
})
现在的问题是我不想要所有 TermRelationships 我只想要将 name 设置为 RandomName 的那些
我尝试了很多我在互联网上找到的方法,但没有任何效果,我最后一次尝试是这样的
Post.findOne({
where: { post_id: 351043 },
include: [
PostMeta,
{
model: TermRelationships,
include: [{ model: TermTaxonomy, where: { name: "RandomName" } }]
}
]
})
但这会返回 null 而不是带有空 TermRelationship 数组的帖子。
我最好的猜测是我应该将 where 子句放在上层并执行以下操作:
Post.findOne({
where: { post_id: 351043 },
include: [
PostMeta,
{
model: TermRelationships,
include: [{ model: TermTaxonomy }],
where: { "$TermTaxonomy.name$": "RandomName" }
}
]
})
但它返回Unknown column 'TermTaxonomy.name' in 'where clause'
TermTaxonomy 有一个名为 name 的列,而这个错误对我来说没有意义。
我是否在这里遗漏了一个重点,或者这应该可以工作,但我可能设置错误?
【问题讨论】:
标签: sql node.js sequelize.js