-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
typeorm One-to-one 관계설정
22.02.15 14:39 작성 조회수 600
0
제로초님 유저개인정보를 담고있는 User Entity와 그외 기타정보를 담고있는 Profile Entity
를 One - to - One 관계로 연결시켜주고자하는데
/user.entity.ts
@OneToOne(() => Profile, (profile) => profile.User, {
cascade: true,
})
@JoinColumn()
Profile: Profile;
유저 엔티티를 이렇게 하고,
/profile.entity.ts
@OneToOne(() => User, (user) => user.Profile)
User: User;
프로파일 엔티티를 이렇게 설정해줬는데,
유저를 생성할때 Profile이 같이 자동생성이 되지않습니다.
혹시나해서 profile에도 cascade:true를 넣어봤는데, 디비 오류가 나네요.
OneToOne관계에서 유저를 생성할때 자동으로 profile이 생성되게할려면 어떻게 해야하나요?
답변을 작성해보세요.
0
조현영
지식공유자2022.02.15
@Column('int', { name: 'ProfileId', nullable: true, comment: '프로필아이디' })
ProfileId: number | null;
@OneToOne(() => Profile, (profile) => profile.User)
@JoinColumn([{ name: 'ProfileId', referencedColumnName: 'id' }])
Profile: Profile;
이런 식으로 명시적으로 ID까지 같이 선언해주시면 좋습니다.
노른자
질문자2022.02.15
흠.. ProfileId를 명시적으로 추가하니 이러한 중복에러가 뜹니다.
[Nest] 34165 - 02/15/2022, 7:54:56 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
QueryFailedError: Duplicate column name 'ProfileId'
노른자
질문자2022.02.15
아 넵 컬럼에 profileId가 잘 생성은 됩니다.
제가 원하는건 User을 생성하면 자동으로 프로필도 생성되서 해당 유저로우에 ProfileId로 할당되는것입니다,
그 방법을 모르겠습니다.
노른자
질문자2022.02.15
const newUser = await this.usersRepository.save(
this.usersRepository.create({
snsId,
provider,
Profile: new Profile(),
}),
);
이런식으로 처리해주니까 원했던대로 되네요!
답변 1