@Entity()
export class Post{
//TypeORM 컬럼에서 배열을 사용하려면 아래의 옵션이 필요합니다.
@Column({ array: true })
commentId: string[];
@OneToMany( (type) => Comment, (comment) => comment )
@JoinColumn({ name: "commentId" })
comment: Comment[];
}
@Entity()
export class Post{
@RelationId( (self: Post) => self.comment)
commentId: string[];
@OneToMany( (type) => Comment, (comment) => comment )
comment: Comment[];
}
@RelationId
데코레이터는 한 방향으로만 정의가 됩니다.
관계
에 있어서 Post
의 comment
가 정해지면 자동으로 comment의 id
가 들어가집니다.
즉, commentId에
직접
데이터를 집어 넣을 수 없습니다.
@RelationId
와 달리 @JoinColumn
의 옵션 name
을 사용하면
@Column
의 속성값과 같을 시 TypeORM에서 둘을 연결 시켜 줍니다.
즉,
commentId
또는comment
둘 다 정의 할 수 있다.만약
commentId
타입이number
라면 `@Column({ unsigned: true}) 옵션을 추가해야한다.
둘 의 차이는 간단히 말하면
단방향 설정이냐 양방향 설정이냐 이다.
내 생각으로는 @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.