wam
w__am 개발노트
wam
  • 분류 전체보기 (165)
    • CS 지식 (10)
      • 자료구조 (0)
      • 알고리즘 (0)
      • 컴퓨터 구조 (0)
      • 운영체제 (0)
      • 네트워크 (7)
      • 데이터베이스 (0)
      • 디자인 패턴 (3)
    • Frontend (131)
      • Three.js (64)
      • NPM (1)
      • Nest.js (19)
      • React (10)
      • Apollo (7)
      • TypeScript (2)
      • JavaScript (12)
      • HTML, CSS (1)
      • Jest (3)
      • E2E (5)
      • Cypress (7)
    • Database (12)
      • TypeORM (12)
    • IT 지식 (8)
      • 클라우드 서비스 (3)
      • 네트워크 (1)
      • 데이터 포맷 (2)
      • 기타 (2)
    • IT Book (2)
    • 유용한 사이트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 🐱 Github

인기 글

태그

  • 함수 리터럴
  • react 성능 최적화
  • 디자인 패턴
  • getdelta()
  • 오프-프레미스(off-premise) 방식
  • gridhelper
  • 함수의 범위
  • isabstract
  • math.cos()
  • 삼각함수
  • math.sin()
  • e.preventdefault()
  • 함수 표현식
  • Interface
  • 렌더링 성능 최적화
  • threejs 개발 할 때 도움을 줄 수 있는 유틸리티
  • mapped types
  • 원형적인 움직임
  • axeshelper
  • Decorators
  • three.js 구성 요소
  • API
  • joi 에러
  • 스코프
  • getelapsedtime()
  • type-graphql
  • reactive variables
  • 초기 환경설정
  • 함수 선언문
  • 데이터 포맷

최근 글

관리자

글쓰기 / 스킨편집 / 관리자페이지
hELLO · Designed By 정상우.
wam

w__am 개발노트

@RelationId, 특정 필드에 대한 외래 키 값만 가져오기
Database/TypeORM

@RelationId, 특정 필드에 대한 외래 키 값만 가져오기

2024. 8. 2. 22:36

 

@RelationId

  • 이 데코레이터는 엔티티의 관계를 사용하여 특정 필드에 대한 외래 키 값만을 직접 가져올 수 있게 해준다.
  • 관계를 전체 로드하지 않고도 필요한 정보만 가져올 수 있다.

 

 

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class Profile {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    gender: string;

    @Column()
    photo: string;
}

 

 

import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, RelationId } from "typeorm";
import { Profile } from "./Profile";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @OneToOne(() => Profile)
    @JoinColumn()
    profile: Profile;

    // 외래 키 값을 직접 참조하는 필드
    @RelationId((user: User) => user.profile)
    profileId: number;
}
  • @OneToOne(() => Profile): User와 Profile 간의 일대일 관계를 정의한다.
  • @JoinColumn(): User 엔티티에서 Profile의 외래 키 열을 정의한다.
  • @RelationId((user: User) => user.profile): User 엔티티에서 profile 관계의 외래 키 값을 가져온다. 이 데코레이터를 사용하면 profileId 필드에 Profile의 id 값이 저장된다.

 

 

 

저작자표시 변경금지

'Database > TypeORM' 카테고리의 다른 글

Find OPtions(LessThan, MoreThan … ) , 데이터를 조회할 때 사용되는 옵션  (0) 2024.08.27
Relations 정리 Many-to-Many, @JoinTable()  (0) 2024.08.06
Column 타입 JSON으로 지정, @Column({ type: "json" })  (0) 2024.08.02
Relations 정리 One-to-One, @JoinColumn()  (0) 2024.08.02
Relations 정리 OneToMany, ManyToOne  (0) 2024.08.02
    'Database/TypeORM' 카테고리의 다른 글
    • Find OPtions(LessThan, MoreThan … ) , 데이터를 조회할 때 사용되는 옵션
    • Relations 정리 Many-to-Many, @JoinTable()
    • Column 타입 JSON으로 지정, @Column({ type: "json" })
    • Relations 정리 One-to-One, @JoinColumn()
    wam
    wam

    티스토리툴바