인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

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

hi2177님의 프로필 이미지
hi2177

작성한 질문수

따라하며 배우는 노드, 리액트 시리즈 - 레딧 사이트 만들기(NextJS)(Pages Router)

useSWR을 이용한 커뮤니티 리스트 가져오기

routes/subs.ts_topSubs 쿼리빌더 관련 질문 있습니다.

작성

·

281

·

수정됨

0

useSWR을 이용한 커뮤니티 리스트 가져오기 강의에서

routes/subs.ts의 topSubs 함수를

const topSubs = async (req: Request, res: Response) => {
  try {
    const imageUrlExp = `COALESCE('s."imageUrn",'https://www.gravatar.com/avatar?d=mp&f=y')`;
    const subs = await AppDataSource.createQueryBuilder()
      .select(
        `s.title, s.name, ${imageUrlExp} as "imageUrl", count(p.id) as "postCount"`
      )
      .from(Sub, "s")
      .leftJoin(Post, "p", `s.name = p."subName"`)
      .groupBy('s.title, s.name, "imageUrl"')
      .orderBy(`"postCount"`, "DESC")
      .limit(5)
      .execute();
    return res.json(subs);
  } catch (error) {
    console.log(error);
    return res.status(500).json({ error: "문제가 발생했습니다." });
  }
};

으로 강사님이 작성해주셨는데, 저는 아래와 같이 해야 동작하더라구요.

const topSubs = async (req: Request, res: Response) => {
  try {
    const imageUrlExp = `COALESCE(s.imageUrn, 'https://www.gravatar.com/avatar?d=mp&f=y')`;
    const subs = await AppDataSource.createQueryBuilder()
      .select(`s.name, s.title, ${imageUrlExp} as "imageUrl", count(p.id) as "postCount"`)
      .from(Sub, 's')
      .leftJoin(Post, 'p', `s.name = p.subName`)
      .groupBy('s.name, s.title, "imageUrl"')
      .orderBy(`"postCount"`, 'DESC')
      .limit(5)
      .execute();
    return res.json(subs);
  } catch (error) {
    console.log(error);
    return res.status(500).json({ error: '문제가 발생했습니다.' });
  }
};

제가 postgresql이 아닌 mariadb를 사용하고 있는데, db가 달라서 생기는 차이가 맞나요? 아니면 다른 이유가 있는지 궁급합니다!

답변 1

1

John Ahn님의 프로필 이미지
John Ahn
지식공유자

안녕하세요!

차이점을 보니 s.name = p.subName 이랑 s.name = p."subName" 차이네요!

이 부분에 대해서는 방금 공식문서에 들어가서 예제나 이에 대한 설명을 찾아봤는데 없네요 ㅠㅠ!!

그래도 생각해보면 orm이 orm 자체에서 정해준 쿼리문법을 이용하면 그걸 각각 디비에 맞게 (mariadb 면 mariadb를 위해서 oracle이면 오라클 디비를 위해서 mongoDB면 mongoDB를 위해서) 바꿔주는데 그 과정에서 약간의 문법 차이가 발생해서 에러가 발생하는 것 같습니다!

감사합니다!

hi2177님의 프로필 이미지
hi2177
질문자

공식문서까지 찾아주시고,, 친절한 답변 감사합니다!

hi2177님의 프로필 이미지
hi2177

작성한 질문수

질문하기