Bird
0
0

What is wrong with this NestJS TypeORM setup?

medium📝 Debug Q14 of 15
NestJS - Database with TypeORM
What is wrong with this NestJS TypeORM setup?
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';

@Module({
  imports: [TypeOrmModule.forRoot({
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'user',
    password: 'pass',
    database: 'testdb',
    entities: [User],
  })],
})
export class AppModule {}
AforRoot() cannot accept configuration objects
BEntities array should be a string path, not class references
CDatabase type 'postgres' is invalid
DMissing synchronize: true to auto-create tables
Step-by-Step Solution
Solution:
  1. Step 1: Check TypeORM configuration options

    While the config is mostly correct, missing synchronize: true means tables won't auto-create on app start, which is common in development.
  2. Step 2: Validate other options

    Entities can be class references, forRoot() accepts config objects, and 'postgres' is a valid database type.
  3. Final Answer:

    Missing synchronize: true to auto-create tables -> Option D
  4. Quick Check:

    synchronize: true enables auto table creation [OK]
Quick Trick: Add synchronize: true for auto table creation in dev [OK]
Common Mistakes:
  • Thinking entities must be string paths
  • Believing forRoot() rejects config objects
  • Assuming 'postgres' is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes