본문 바로가기

728x90

TypeORM

(7)
주의 사항 : 릴레이션 아이디 삭제 현상 @ManyToOne -> @OneTOne 관계로 변경하면 RelationId가 변경된다.
QueryFailedError 에러 현상QueryFailedError: insert or update on table "order_item" violates foreign key constraint "FK_904370c093ceea4369659a3c810" - 이유: FK_904370c093ceea4369659a3c810 키 값으로 product 제품의 id (PK)컬럼을 참조하고 있다는 의미   ProductId는 어디서 왔을까? order_item테이블이 외부 키 ->  product테이블의 id컬럼 (기본 키)  order_item과 product 테이블 n:1 관계에 의해  order_item테이블이 주인이 되고 product테이블의 id( productId )를 가진다.
fineOne의 옵션 relations SQL에서 테이블을 조인하는 방법을 TypeORM에서 어떻게 구현할까? 1. relation 설정이 필요하다. 아래의 예시는 Order 엔티티의 cutomer(Member Entity ) import { CoreEntity } from 'src/common/entites/core.entity'; import { Deal } from 'src/deals/entitles/deal.entity'; import { Member } from 'src/member/entites/member.entity'; import { Column, Entity, ManyToOne} from "typeorm"; @Entity() export class Order extends CoreEntity { @ManyToOne( () =..
relation table 같이 삭제하기 외래키로 참조하고 있는 테이블의 경우 참조되고 있는 테이블에서 현재 부모 테이블에서 참조되고 있으니 삭제가 안된다고 에러가 날 수있다. 그렇다면 어떻게 해결해야 될까? 준비 typeORM Nestjs 프레임 워크(typescript) 우선 아래의 예시를 통해서 OneToMany와 ManyToOne 의 관계를 이해를 해보자. /* eslint-disable prettier/prettier */ import { Length } from "class-validator"; import { CoreEntity } from "src/common/entites/core.entity"; import { Member } from "src/member/entites/member.entity"; import { Order ..
TypeORM OneToMany란? 1. 정의 OneToMany는 Eintity1이 Entity2의 다수 인스턴스들과의 관계이다. 2. Member 엔티티 설정 (Entity 1) export class Member { @OneToMany(✅ type => Business, business => business.owner //✅ inverseSide ) business:Business[]; } 3. Business 엔티티 설정 (Entity 2) Entity2 is ⭐the owner of the relationship, and stores the id of Entity1 on its side of the relation. ( Business 엔티티 인스턴스들이 Member엔티티와의 관계에서 주인이 된다.) import { Column, ..
@BeforeInsert와 @BeforeUpdate()의 차이 TypeORM을 사용하면서 시간이 지나고 이해가 된 부분을 한 번 설명해 보겠다! @BeforeInsert : " 이 데코레이터는 hashingPw 함수가 this.password를 DB에 삽입 하기 전에 실행한다" @BeforeUpdate: "이 데코레이터는 hashingPw 함수가 this.password를 DB에 password 컬럼 값을 업데이트 즉 변경할 경우 실행을 한다. 똬! " import { CoreEntity } from 'src/common/entites/core.entity'; import { BeforeInsert, BeforeUpdate, Column, Entity } from 'typeorm'; import * as bcrypt from "bcrypt"; import { Int..
NestJS TypeORM 설정 NestJS는 TypeORM을 지원한다. 그렇다면 SQL을 쓰지 않고 사용한다? 그런데 ORM이 무엇인 지 ? ORM이란? " 객체와 관계형 DB의 데이터를 자동으로 매핑 해준다. " 글로는 이해가 잘 가지 않아 실제 설정과 함께 매핑이 되는 과정을 보면 쉽게 이해가 가능하다. 1. 일단 node에서 TypeORM 을 다운받는다. NestJS의 공식문서를 보면 이렇게 나온다. TypeORM은 Typescript에 가정 최적화되어 있다고 알 수 있다. 공식 홈페이지는 mysql로 예시를 들고 있다. 필자는 postgresql을 사용하기 때문에 npm install --save @nestjs/typeorm typeorm pg 이렇게 다운을 받았다. 상세하게 어떻게 설치하는 지에 대한 정보는 npm 홈피 ht..

728x90