limit에 대해 질문드립니다.
165
작성한 질문수 49
const profilePosts = await User.findOne({
where: { id: userId },
attributes: [],
include: [
{
model: Post,
include: [
{
model: User,
attributes: ["id", "nickname", "department", "avatar"],
},
{
model: User,
as: "PostLikeUsers",
attributes: ["id"],
},
{
model: Comment,
attributes: ["id"],
},
],
},
],
});include 안에서 Post model에 limit을 두고 싶습니다.
예를 들어, Post model에 16개의 게시글이 저장되어 있으면 5개만 불러오고 싶습니다.
어떻게 하면 될까요?
시도했던 방법
1.
include: [
{
model: Post,
limit: 5,
subQuery: false,
include: [
{
model: User,
attributes: ["id", "nickname", "department", "avatar"],
},
{
model: User,
as: "PostLikeUsers",
attributes: ["id"],
},
{
model: Comment,
attributes: ["id"],
},
],
},
],
2.
include: [
{
model: Post,
attributes: ["id", "UserId"],
separate: true,
limit: 5,
include: [
{
model: User,
attributes: ["id", "nickname", "department", "avatar"],
},
{
model: User,
as: "PostLikeUsers",
attributes: ["id"],
},
{
model: Comment,
attributes: ["id"],
},
],
},
],
답변 1
1
separate를 해도 안된다면 쿼리를 두번에 나눠서 한 후 합치는 수밖에 없습니다. 애초에 separate가 그 역할입니다.
0
User가 작성한 Post 게시글을 가져오는 것이라 User model에서는 Post model의 정보를 알 수 없습니다. 어떻게 나눠서 합치면 되는지 조언 부탁드립니다.
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
email: {
type: DataTypes.STRING(30),
allowNull: false,
unique: "email",
},
name: {
type: DataTypes.STRING(30),
allowNull: false,
},
nickname: {
type: DataTypes.STRING(30),
allowNull: false,
unique: "nickname",
},
department: {
type: DataTypes.STRING(30),
allowNull: false,
},
avatar: {
type: DataTypes.TEXT,
},
},
{
charset: "utf8",
collate: "utf8_general_ci",
}
);
User.associate = (db) => {
db.User.hasMany(db.Post);
};
}
module.exports = (sequelize, DataTypes) => {
const Post = sequelize.define(
"Post",
{
type: {
type: DataTypes.STRING(30),
allowNull: false,
},
title: {
type: DataTypes.TEXT,
allowNull: false,
},
content: {
type: DataTypes.TEXT,
allowNull: false,
},
views: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
},
{
charset: "utf8mb4",
collate: "utf8mb4_general_ci",
}
);
Post.associate = (db) => {
db.Post.belongsTo(db.User);
};
}
User

Post

넥스트 버젼 질문
0
90
2
로그인시 401 Unauthorized 오류가 뜹니다
0
104
1
무한 스크롤 중 스크롤 튐 현상
0
197
1
특정 페이지 접근을 막고 싶을 때
0
116
2
createGlobalStyle의 위치와 영향범위
0
103
2
인라인 스타일 리렌더링 관련
0
98
2
vsc 에서 npm init 설치시 오류
0
159
2
nextjs 15버전 사용 가능할까요?
0
166
1
화면 새로고침 문의
0
129
1
RTK에서 draft, state 차이가 있나요?
0
164
2
Next 14 사용해도 될까요?
0
455
1
next, node 버전 / 폴더 구조 질문 드립니다.
0
359
1
url 오류 질문있습니다
0
218
1
ssh xxxxx로 우분투에 들어가려니까 port 22: Connection timed out
0
391
1
sudo certbot --nginx 에러
0
1295
2
Minified React error 콘솔에러 (hydrate)
0
481
1
카카오 공유했을 때 이전에 작성했던 글이 나오는 버그
0
257
1
프론트서버 배포 후 EADDRINUSE에러 발생
0
341
1
npm run build 에러
0
526
1
front 서버 npm run build 중에 발생한 에러들
0
399
1
서버 실행하고 브라우저로 들어갔을때 404에러
0
351
2
css 서버사이드 랜더링이 적용되지 않아서 문의 드립니다.
0
291
1
팔로워 3명씩 불러오고 데이터 합쳐주는걸로 바꾸고 서버요청을 무한으로하고있습니다.
0
250
2
해시태그 검색에서 throttle에 관해 질문있습니다.
0
207
1





