Javascript 计算Sequelize.js中联接表之间的多对多表中的关系
我正在使用sequelize.js构建一个项目,其中包括一个标签表和一个故事表。他们有一个多对多的关系,我在sequelize中创建了一个贯穿StoryTag的表。到目前为止,这一切都非常有效,但我想获得大多数popluar标记的列表,比如它们在StoryTag表中关联了多少个故事,并根据使用此标记的故事数对它们进行排序 这就是我要做的MySQL语法。这在MySQL Workbench中非常有效:Javascript 计算Sequelize.js中联接表之间的多对多表中的关系,javascript,mysql,node.js,sequelize.js,Javascript,Mysql,Node.js,Sequelize.js,我正在使用sequelize.js构建一个项目,其中包括一个标签表和一个故事表。他们有一个多对多的关系,我在sequelize中创建了一个贯穿StoryTag的表。到目前为止,这一切都非常有效,但我想获得大多数popluar标记的列表,比如它们在StoryTag表中关联了多少个故事,并根据使用此标记的故事数对它们进行排序 这就是我要做的MySQL语法。这在MySQL Workbench中非常有效: SELECT tagName, COUNT(StoryTag.TagId) FROM Tags
SELECT tagName, COUNT(StoryTag.TagId)
FROM Tags
LEFT JOIN StoryTag on Tags.id = StoryTag.TagId
GROUP BY Tags.tagName ORDER BY COUNT(StoryTag.TagId) DESC;
这就是sequelize.js中的工作原理。这是一个原始的查询,并不理想,但因为它不处理任何敏感信息,所以它不是一个大问题,只是非常不雅观
//DIRECT QUERY METHOD (TEST)
app.get("/api/directags", function (req, res) {
db.sequelize.query("select tags.id, tags.TagName, COUNT(stories.id) as num_stories
from tags left join storytag on storytag.TagId = tags.id
left join stories on storytag.StoryId = stories.id
group by tags.id order by num_stories desc;", {
type: db.Sequelize.QueryTypes.SELECT
}).then(function(result) {
res.send(result);
});
});
这个输出
[
{
"id": 3,
"TagName": "fiction",
"num_stories": 3
},
{
"id": 5,
"TagName": "Nursery Rhyme",
"num_stories": 2
},
...
{
"id": 4,
"TagName": "nonfiction",
"num_stories": 0
}
]
应该如此不太起作用的是:
//Sequelize count tags
//Known issues: will not order by the count
//Includes a random 'storytag' many-to-many table row for some reason
app.get("/api/sequelizetags", function (req, res) {
db.Tag.findAll({
attributes: ["id","TagName"],
include: [{
model: db.Story,
attributes: [[db.sequelize.fn("COUNT", "stories.id"), "Count_Of_Stories"]],
duplicating: false
}],
group: ["id"]
}).then(function (dbExamples) {
res.send(dbExamples);
});
});
哪些产出:
[
{
"id": 1,
"TagName": "horror",
"Stories": [
{
"Count_Of_Stories": 1,
"StoryTag": {
"createdAt": "2018-11-29T21:09:46.000Z",
"updatedAt": "2018-11-29T21:09:46.000Z",
"StoryId": 1,
"TagId": 1
}
}
]
},
{
"id": 2,
"TagName": "comedy",
"Stories": []
},
{
"id": 3,
"TagName": "fiction",
"Stories": [
{
"Count_Of_Stories": 3,
"StoryTag": {
"createdAt": "2018-11-29T21:10:04.000Z",
"updatedAt": "2018-11-29T21:10:04.000Z",
"StoryId": 1,
"TagId": 3
}
}
]
},
{
"id": 4,
"TagName": "nonfiction",
"Stories": []
},
...
{
"id": 8,
"TagName": "Drama",
"Stories": [
{
"Count_Of_Stories": 1,
"StoryTag": {
"createdAt": "2018-11-30T01:13:56.000Z",
"updatedAt": "2018-11-30T01:13:56.000Z",
"StoryId": 3,
"TagId": 8
}
}
]
},
{
"id": 9,
"TagName": "Tragedy",
"Stories": []
}
]
这是不正常的,故事的计数被埋葬了。这似乎是来自数据库的常见和频繁的请求,但我不知道如何使用sequelize.js正确地执行这一操作使我失败的资源:
sequelize的官方文档:
sequelize的一些不太正式、可读性更高的文档:
num_stories
-Count_Of_stories
,等等)订购
count
,以便在实例的顶级获取它包含[]。在文档中复制选项
db.Tag.findAll({
attributes: [
"id",
"TagName",
[db.sequelize.fn("COUNT", "stories.id"), "Count_Of_Stories"]
],
include: [{
model: db.Story,
attributes: [],
duplicating: false
}],
group: ["id"],
order: [
[db.sequelize.literal("`Count_Of_Stories`"), "DESC"]
]
});
以下是最终有效的方法,以防其他人有这个问题。我们还为include故事添加了一个where,但这是可选的。 此资源比官方sequelize文档更容易理解:
我还了解到,在使用sequelize时,熟悉承诺真的很有帮助
db.Tag.findAll({
group: ["Tag.id"],
includeIgnoreAttributes:false,
include: [{
model: db.Story,
where: {
isPublic: true
}
}],
attributes: [
"id",
"TagName",
[db.sequelize.fn("COUNT", db.sequelize.col("stories.id")), "num_stories"],
],
order: [[db.sequelize.fn("COUNT", db.sequelize.col("stories.id")), "DESC"]]
}).then(function(result){
return result;
});
在选项中使用
到:{attributes:[]}
这将返回“SequelizeDatabaseError:Unknown column'Tag.Count_Of_Stories'in'order子句”。@TheresaB fixed。