PracticeEveryday
TypeORM 1:1 본문
OneToOne
- 유저, 프로필 테이블은 1:1 관계이다.
- 유저에서는 타입을 프로필 테이블로, 프로필에서는 타입을 유저로 지정해줍니다.
- 그리고 @JoinColumn()을 사용한 필드는 FK( 외래 키 )로 타겟 테이블( 유저 )에 등록되게 됩니다.
@JoinColumn()은 반드시 한 쪽 테이블에서만 사용해야 합니다.
import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToOne
} from "typeorm";
import { ProfileEntity } from "./profile.entity";
@Entity("user")
export class UserEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ name: "email", unique: true, nullable: false })
email: string;
@Column({ nullable: false })
password: string;
@Column({ nullable: false })
name: string;
@OneToOne((type) => ProfileEntity, (ProfileEntity) => ProfileEntity.user_id)
profile: ProfileEntity;
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
OneToOne,
JoinColumn,
} from "typeorm";
import { UserEntity } from "./user.entity";
@Entity("profile")
export class ProfileEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ name: "subject", type: "varchar", unique: true, nullable: false })
subject: string;
@Column({ name: "description", type: "varchar", nullable: false })
description: string;
@CreateDateColumn({ name: "create_dt", type: "timestamptz", nullable: false })
create_dt: Date;
// 수정일
@UpdateDateColumn({ name: "update_dt", type: "timestamptz", nullable: false })
update_dt: Date;
@OneToOne((type) => UserEntity, (UserEntity) => UserEntity.profile)
@JoinColumn()
user_id: UserEntity;
}
- @JoinColumn()은 불러오는 테이블이 아닌 당하는 쪽의 엔터티에 넣어주어야 한다.
- 위의 경우 잘나온다!
import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToOne,
JoinColumn,
} from "typeorm";
import { ProfileEntity } from "./profile.entity";
@Entity("user")
export class UserEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ name: "email", unique: true, nullable: false })
email: string;
@Column({ nullable: false })
password: string;
@Column({ nullable: false })
name: string;
@OneToOne((type) => ProfileEntity, (ProfileEntity) => ProfileEntity.user_id)
@JoinColumn()
profile: ProfileEntity;
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
OneToOne,
JoinColumn,
} from "typeorm";
import { UserEntity } from "./user.entity";
@Entity("profile")
export class ProfileEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ name: "subject", type: "varchar", unique: true, nullable: false })
subject: string;
@Column({ name: "description", type: "varchar", nullable: false })
description: string;
@CreateDateColumn({ name: "create_dt", type: "timestamptz", nullable: false })
create_dt: Date;
// 수정일
@UpdateDateColumn({ name: "update_dt", type: "timestamptz", nullable: false })
update_dt: Date;
@OneToOne((type) => UserEntity, (UserEntity) => UserEntity.profile)
user_id: UserEntity;
}
- 하지만 @JoinColumn()을 반대 Entity에 넣으면 안나온당 ㅠㅠ
const foundUser = await this.userRepository.findOne({
where: {
id: userId,
},
relations: {
profile: true,
},
});
const foundUser2 = await this.userRepository
.createQueryBuilder("user")
.leftJoinAndSelect("user.profile", "profile")
.getOne();
- Left Join 하는 두가지 방법!
[TypeORM] TypeORM 관계 설정하기
들어가며 TypeORM에 대해 아무런 지식이 없습니다. 하지만 NestJS로 개발을 하려면, TypeORM에 대한 깊은 이해가 필요하겠다고 생각했습니다. 잘 모르지만, 삽질해가면서 TypeORM에 대해 공부해보고자
overcome-the-limits.tistory.com
'DB' 카테고리의 다른 글
TypeORM (0) | 2022.08.20 |
---|---|
TypeORM (0) | 2022.08.13 |
논리적 데이터 모델링 (0) | 2022.08.09 |
개념적 데이터 모델링 (0) | 2022.08.09 |
RDB Modeling (0) | 2022.08.08 |
Comments