Sequelize - 计算关联并从与查询的关联中获取数据
Sequelize - Counting associations and fetching data from association with query
我正在使用 sequelize(将 Postgres 作为数据库)创建一个类似 Twitter 的应用程序。有 3 tables - 用户、posts 和 postlikes。现在,在列出 post 时,我想获取喜欢的总数,以及当前用户是否喜欢 post。那么如何使用 sequelize 实现这一点呢?
我已经创建了一个简单的查询,但不知道下一步该做什么。 post喜欢 table 有 postId 和 userId。我将用户包含在下面的查询中,以获取 post.
作者的数据
let allPosts = await db.Post.findAndCountAll({
order: [['createdAt', 'DESC']],
offset: offset * limit,
limit: limit,
include: [
{
model: db.User,
as: 'user',
attributes: ['id', 'firstName', 'lastName', 'username', 'profilePic'],
raw: true,
},
{
model: db.PostLike,
as: 'postlikes',
raw: true,
},
],
});
我在 Whosebug 上找到了以下代码并尝试但无法得到任何结果:
attributes: [
[
db.Sequelize.fn('COUNT', db.Sequelize.col('postlikes.id')),
'likeCount',
],
],
收到此错误 - 原始:错误:缺少 table“post喜欢”
的 FROM 子句条目
所以请帮助我。提前致谢。
型号:Post.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Post extends Model {
static associate(models) {
this.hasMany(models.PostLike, {
foreignKey: "postId",
as: "postlikes",
});
}
}
Post.init({
type: { type: DataTypes.STRING },
}, {
sequelize,
modelName: "Post",
});
return Post;
};
型号:PostLike.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class PostLike extends Model {
static associate(models) {
this.belongsTo(models.Post, {
foreignKey: "postId",
as: "post",
});
}
}
PostLike.init({
postId: { type: DataTypes.INTEGER },
}, {
sequelize,
modelName: "PostLike",
});
return PostLike;
};
控制器
var post = await db.Post.findAll({
attributes: [
'id',
[db.Sequelize.fn('COUNT', db.Sequelize.col('postlikes.id')), 'likeCount',],
],
include: ['postlikes']
})
console.log(JSON.parse(JSON.stringify(post)))
process.exit()
输出:
[ { id: 1, likeCount: 56, postlikes: [ [Object] ] } ]
我正在使用 sequelize(将 Postgres 作为数据库)创建一个类似 Twitter 的应用程序。有 3 tables - 用户、posts 和 postlikes。现在,在列出 post 时,我想获取喜欢的总数,以及当前用户是否喜欢 post。那么如何使用 sequelize 实现这一点呢?
我已经创建了一个简单的查询,但不知道下一步该做什么。 post喜欢 table 有 postId 和 userId。我将用户包含在下面的查询中,以获取 post.
作者的数据let allPosts = await db.Post.findAndCountAll({
order: [['createdAt', 'DESC']],
offset: offset * limit,
limit: limit,
include: [
{
model: db.User,
as: 'user',
attributes: ['id', 'firstName', 'lastName', 'username', 'profilePic'],
raw: true,
},
{
model: db.PostLike,
as: 'postlikes',
raw: true,
},
],
});
我在 Whosebug 上找到了以下代码并尝试但无法得到任何结果:
attributes: [
[
db.Sequelize.fn('COUNT', db.Sequelize.col('postlikes.id')),
'likeCount',
],
],
收到此错误 - 原始:错误:缺少 table“post喜欢”
的 FROM 子句条目所以请帮助我。提前致谢。
型号:Post.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Post extends Model {
static associate(models) {
this.hasMany(models.PostLike, {
foreignKey: "postId",
as: "postlikes",
});
}
}
Post.init({
type: { type: DataTypes.STRING },
}, {
sequelize,
modelName: "Post",
});
return Post;
};
型号:PostLike.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class PostLike extends Model {
static associate(models) {
this.belongsTo(models.Post, {
foreignKey: "postId",
as: "post",
});
}
}
PostLike.init({
postId: { type: DataTypes.INTEGER },
}, {
sequelize,
modelName: "PostLike",
});
return PostLike;
};
控制器
var post = await db.Post.findAll({
attributes: [
'id',
[db.Sequelize.fn('COUNT', db.Sequelize.col('postlikes.id')), 'likeCount',],
],
include: ['postlikes']
})
console.log(JSON.parse(JSON.stringify(post)))
process.exit()
输出:
[ { id: 1, likeCount: 56, postlikes: [ [Object] ] } ]