개발 블로깅/오늘의 TIL

[2019.06.06] 오늘의 TIL - TypeORM relation column update하는 방법

Hello이뇽 2019. 6. 6. 20:12

난 오늘도 열심히 삽질을 한다....

passport도 나를 그렇게 힘들게 하더니... 이제 API를 좀 만들어 보려고 하니까 TypeORM이 나를 괴롭힌다..흑흑..


 아래와 같은 테이블이 구성되어 있다고 가정한다.

post.ts

class post extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;
  
  @ManyToOne(type => Categories, categories => categories.post)
  categories : Categories
}

테이블 [id int(11), categoriesId int(11)]

그러면 post를 select 할 때는, 실제 테이블에서 칼럼명이 categoriesId인 것이 categories로 associate 되어 id에 해당하는 categories 데이터들을 배열 형식으로 가져올 것이다.

 

문제는 해당 post테이블의 categoriesId 값을 변경할 때 일어났다..

# 기본적인 update 구문

await createQueryBuilder()
.update(테이블명)
.set({칼럼명:변경할 값})
.execute();

 

# 내가 시도한 구문

await createQueryBuildet()
.update(post)
.set({
  categoriesId: 1
});

그러나 아래와 같은 메세지가 떴다..

EntityColumnNotFound: No entity column "categoriesId" was found

칼럼이 존재 하지 않는단다. 테이블 내 칼럼명은 'categoriesId' 이지만, 클래스 안의 속성명은 'categories'이기 때문인 것 같았다.

그리고 post 테이블의 categories속성 타입이 Categories 클래스 타입이기 때문에, 아래와 같이도 한번 시도를 해봤다.

const categories = new categories();
categories.id = 1
await createQueryBuildet()
.update(post)
.set({
  categories: categories
});

그리고 아래와 같은 메세지를 받았다...

EntityColumnNotFound: No entity column "categories" was found

테이블 칼럼명, association 속성명 둘다 없댄다..-0-;;

 

# 구글링 검색해본 키워드 

  • typeORM EntityColumnNotFound
  • typeORM relation column update 
  • update no entity column was found
  • 외 여러가지 등등..

근데 내가 겪고있는 문제의 내용이 정말 없다... 하나하나 다 살펴봐도 나와 다른 문제의 내용들...

페이지 8번쯤 넘어갔을 때, 한 줄의 코드내용을 보았다...

@ManyToOne(type => Users, user => user.event)
user: Users;
userId: Number

오잉..!?! 속성명을 저렇게 두개로 놓을 수 있던 거였어..?!

바로 적용 시도해본다.

post.ts

class post extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;
  
  @ManyToOne(type => Categories, categories => categories.post)
  categories : Categories
  categoriesId: Number
}
await createQueryBuildet()
.update(post)
.set({
  categoriesId: 1
});

아래와 같은 메세지를 받았다..

EntityColumnNotFound: No entity column "categoriesId" was found

안된다..;;;

허탈감과 짜증이 엄청나게 느낄 쯤...괜히 한번 관계 설정을 지우고, categorisId 속성으로 부여해서 시도해본다..당연히 될거다.

class post extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;
  
  @Column()
  categoriesId: Number
}
await createQueryBuildet()
.update(post)
.set({
  categoriesId: 1
});

당연히 잘 적용되었다...

 

어..?!!?

 

이 심상치 않은 느낌....관계설정 바로 밑에 Column속성으로 넣어서 시도해본다..

class post extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;
  
  @ManyToOne(type => Categories, categories => categories.post)
  categories: Categories
  @Column()
  categoriesId: Number
}
await createQueryBuildet()
.update(post)
.set({
  categoriesId: 1
});

적용됐다..;;;;

그냥 테이블 칼럼명에 맞게, 속성명 똑같이 하나 더 만들어서 시도하면 된다..

난 왜 꼭 관계 설정 association을 이용해서 해야된다고 생각했을까..흑흑..

반응형