RelationId와 JoinColumn id??

00_8_3·2021년 4월 11일
0

Relation id 명시의 방법

  • 1
@Entity()
export class Post{
    //TypeORM 컬럼에서 배열을 사용하려면 아래의 옵션이 필요합니다.
  	@Column({ array: true })
  	commentId: string[];

	@OneToMany( (type) => Comment, (comment) => comment )
  	@JoinColumn({ name: "commentId" })
    	comment: Comment[];
}
  • 2
@Entity()
export class Post{
	
  	@RelationId( (self: Post) => self.comment)
  	commentId: string[];
  	
	@OneToMany( (type) => Comment, (comment) => comment )
    	comment: Comment[];
}

둘의 차이는 무엇인가??

RelationId

@RelationId 데코레이터는 한 방향으로만 정의가 됩니다.
관계에 있어서 Postcomment가 정해지면 자동으로 comment의 id가 들어가집니다.

즉, commentId에 직접 데이터를 집어 넣을 수 없습니다.

JoinColumn({ name : "" })

@RelationId와 달리 @JoinColumn의 옵션 name을 사용하면
@Column의 속성값과 같을 시 TypeORM에서 둘을 연결 시켜 줍니다.

즉, commentId 또는 comment 둘 다 정의 할 수 있다.

만약 commentId 타입이 number라면 `@Column({ unsigned: true}) 옵션을 추가해야한다.

출처

RelationId 란

마침

둘 의 차이는 간단히 말하면
단방향 설정이냐 양방향 설정이냐 이다.

내 생각으로는 @RelationId가 단방향이라 성능적으로 더 좋아 보이는데
Issue RelationId
이슈가 있어보입니다.. 조금 더 찾아보고 추가 할 듯 합니다.

추가

  • @JoinColumn

Defines which side of the relation contains the join column with a foreign key and allows you to customize the join column name and referenced column name. Example:

  • @RelationId

Loads id (or ids) of specific relations into properties. For example, if you have a many-to-one category in your Post entity, you can have a new category id by marking a new property with @RelationId. Example:

This functionality works for all kind of relations, including many-to-many:

Relation id is used only for representation. The underlying relation is not added/removed/changed when chaining the value.

DOCS

0개의 댓글