반응형
문제
Show라는 엔티티를 app.module.ts에 추가하고 서버를 구동시켰을 때 에러가 발생했다.
ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
TypeORMError: Entity metadata for Show#dates was not found. Check if you specified a correct entity object and if it's connected in the connection options.
이 에러는... Show 엔티티에 대해 정의된 dates 관계가 데이터베이스 연결에서 인식되지 않았다는 것을 나타낸다고 함...
원인
// app.module.ts
...
import { AuthModule } from './auth/auth.module';
import { User } from './user/entities/user.entity';
import { UserModule } from './user/user.module';
import { Show } from './show/entities/show.entity';
import { ShowModule } from './show/show.module';
const typeOrmModuleOptions = {
useFactory: async (
configService: ConfigService,
): Promise<TypeOrmModuleOptions> => ({
namingStrategy: new SnakeNamingStrategy(),
type: 'mysql',
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
database: configService.get('DB_NAME'),
entities: [User, Show],
synchronize: configService.get('DB_SYNC'),
logging: true,
}),
inject: [ConfigService],
};
...
Show entity만 선언해주고 같은 파일 안에 있는 다른 entity들은 선언해주지 않아서 에러가 발생한 것...ㅠ
그래서 dates 관계에 대한 인식이 되지 않았다.
해결
// app.module.ts
...
import { AuthModule } from './auth/auth.module';
import { User } from './user/entities/user.entity';
import { UserModule } from './user/user.module';
import { Show, ShowDate, Seat } from './show/entities/show.entity';
import { ShowModule } from './show/show.module';
const typeOrmModuleOptions = {
useFactory: async (
configService: ConfigService,
): Promise<TypeOrmModuleOptions> => ({
namingStrategy: new SnakeNamingStrategy(),
type: 'mysql',
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
database: configService.get('DB_NAME'),
entities: [User, Show, ShowDate, Seat],
synchronize: configService.get('DB_SYNC'),
logging: true,
}),
inject: [ConfigService],
};
...
이렇게 연관된 모든 엔티티를 불러와주니 문제 없이 잘 돌아감
반응형
'TIL' 카테고리의 다른 글
[cloudfront] 이미지 CDN과 리사이징 (0) | 2024.04.04 |
---|---|
[Websocket] 알람 및 채팅 기능 구현 (0) | 2024.03.19 |
[TypeORM] TypeORM에서 자동으로 컬럼명이 바뀌는 문제 (0) | 2024.03.12 |
Nest.js 프로젝트에 TypeORM 적용하기 (0) | 2024.03.08 |
lodash 라이브러리 함수 정리 (0) | 2024.03.08 |