강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

바스니카님의 프로필 이미지
바스니카

작성한 질문수

[리뉴얼] React로 NodeBird SNS 만들기

쿼리스트링과 lastId 방식

limit에 대해 질문드립니다.

작성

·

156

0

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가 그 역할입니다.

바스니카님의 프로필 이미지
바스니카
질문자

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

image

Post

image

 

 

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

Post 모델에 UserId가 있으니 그걸로 가져오시면 됩니다.

바스니카님의 프로필 이미지
바스니카

작성한 질문수

질문하기