강의

멘토링

로드맵

Inflearn コミュニティ Q&A

karmesin9247996 のプロフィール画像
karmesin9247996

投稿した質問数

[コードキャンプ]ブートキャンプで作られた高濃縮バックエンドコース

10-01 Entity 実装

ManyToMany 테이블이 자동으로 만들어지지 않습니다..

解決済みの質問

作成

·

437

0

 

import { ProductCategory } from 'src/apis/productsCategories/entites/productCategory.entity';
import { ProductSaleslocation } from 'src/apis/productsSaleslocations/entities/productSaleslocation.entity';
import { ProductTag } from 'src/apis/productsTags/entities/productTag.entity';
import { User } from 'src/apis/users/entities/user.entity';
import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Product {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @Column()
  description: string;

  @Column()
  price: number;

  @Column({ default: false })
  isSoldout: boolean;

  @JoinColumn() // 1:1 연결에서는 두 테이블 중 중심을 정하는 JoinColumn을 달아주어야한다.
  @OneToOne(() => ProductSaleslocation) // 일대일 연결. 어떤 테이블이랑 연결될지 표기. ProductSaleslocation 테이블과 연결 할 것이다.
  productSaleslocation: ProductSaleslocation; // 그 때 사용되는 Column은 productSaleslocation이고 타입은 다음과 같다, FK

  @ManyToOne(() => ProductCategory) // many가 Product 한개인게 Category
  productCategory: ProductCategory; // FK

  @ManyToOne(() => User)
  user: User;

  @JoinTable() // ManyToMany는 둘 중 하나에 JoinTable 작성
  @ManyToMany(() => ProductTag, (productTags) => productTags.products) // 상대방 입장에서 나를 볼 때 products
  productTags: ProductTag[]; // 객체가 여러개이기 떄문에 객체 배열타입 사용
}

 

import { Product } from 'src/apis/products/entities/product.entity';
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class ProductTag {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @ManyToMany(() => Product, (products) => products.productTags)
  products: Product[];
}

ManyToMany 설정했는데.. product_product_tags_product_tag 테이블이 자동으로 생성이 안되네요. 코드 말고 건드려줘야 할 부분이 있나요?

javascriptnode.jsdockerrest-apinestjs

クイズ

46%が間違えています。挑戦してみましょう!

TypeORM과 같은 ORM(Object-Relational Mapping) 라이브러리의 주된 역할은 무엇일까요?

데이터베이스 스키마를 자동으로 설계합니다.

객체 지향 프로그래밍 언어의 객체와 관계형 데이터베이스의 데이터를 매핑합니다.

프론트엔드와 백엔드 서버 간의 통신 프로토콜을 표준화합니다.

API 요청에 대한 응답 형식을 정의하고 관리합니다.

回答 2

1

karmesin924님의 프로필 이미지
karmesin924
質問者

우왁..!! 바로 해결됐습니다. 감사합니다!!

0

nwd09074926님의 프로필 이미지
nwd09074926
インストラクター

안녕하세요! karmesin924님!

올려주신 코드는 잘 작성해 주신 것 같아요!
데이터베이스 이름을 선택하여 마우스 오른쪽버튼을 눌러서 새로고침을 한 번 해보시겠어요?!

아무데서나 새로고침하시면 안되시구, 데이터베이스명을 선택하여 새로고침을 해보세요!

karmesin9247996 のプロフィール画像
karmesin9247996

投稿した質問数

質問する