일기/에러일기

[에러일기] 12회차 - Nest.js 엔티티 연결시 에러발생

표자 2023. 3. 31. 15:55
728x90
반응형

1. 문제

ban.entity.ts를 새로 만들어서 데이터베이스에 추가하려 했으나 에러문구 발생

 

[Nest] ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... 
TypeORMError: Entity metadata for User#bans was not found. 
Check if you specified a correct entity object and 
if it's connected in the connection options.

 

2. 시도해본 것들

엔티티..메타데이터..not found.. 자주봤던 것 같다.

일단 entity를 만들고, .env에 있는 DATABASE_SYNC=true로 만들면 자동으로 동기화가 될 거라 생각했다.

 

3. 해결과정

그러나!! 이 모든 것이 진행되는 config의 typeorm.config.service.ts에서 Ban 엔티티를 추가해줘야한다.

@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
  constructor(private readonly configService: ConfigService) {} //필수

  createTypeOrmOptions(): TypeOrmModuleOptions {
    return {
      type: 'mysql',
      host: this.configService.get<string>('DATABASE_HOST'),
      port: this.configService.get<number>('DATABASE_PORT'),
      username: this.configService.get<string>('DATABASE_USERNAME'),
      password: this.configService.get<string>('DATABASE_PASSWORD'),
      database: this.configService.get<string>('DATABASE_NAME'),
      namingStrategy: new SnakeNamingStrategy(),
      logging: Boolean(this.configService.get<string>('DATABASE_logging')),
      synchronize: Boolean(this.configService.get<string>('DATABASE_SYNC')), // 배포 시 false
      entities: [
        Collection,
        CollectionItem,
        Comment,
        CommentLike,
        CommentUserTag,
        Post,
        Hashtag,
        Image,
        PostLike,
        PostUserTag,
        Restaurant,
        User,
        Follow,
        Reports,
        Ban,
      ],
    };
  }
}

 

4. 알게 된 점

엔티티 작성, DATABASE_SYNC=true 뿐만아니라 config에서

entities에도 엔티티가 추가됬음을 알려줘야 한다.

728x90
반응형