Skip to content
Advertisement

Nest JS Table is not creating. Written entities

Team,

I have followed Nest.js and TypeORM documentations. Somehow tables are not generating in PostgreSQL. The database is connected successfully.

Nest Server running without error. Issue: The table is not generating.

src/tag/tag.entity.ts

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('tags')
export class Tag {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}

src/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from '@app/app.controller';
import { AppService } from '@app/app.service';
import { TagModule } from '@app/tag/tag.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Tag } from './tag/tag.entity';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [
        ConfigModule.forRoot({
          isGlobal: true,
          envFilePath: '.local.env',
          // envFilePath: ".prod.env",
        }),
      ],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('HOST'),
        port: +configService.get('PORT'),
        username: configService.get('USERNAME'),
        password: configService.get('PASSWORD'),
        database: configService.get('DATABASE'),
        entities: [Tag],
        // entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: configService.get<boolean>('SYNC'),
      }),
      inject: [ConfigService],
    }),
    TagModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

.local.env

# DATABASE CREDETIALS
HOST=localhost
PORT=5432
USERNAME=mediumclone
PASSWORD=8170954991
DATABASE=postgres
SYNC=true

Please help me generate Postgres tables. Framework – Nest JS Database – PostgreSQL

enter image description here

enter image description here

enter image description here

enter image description here

Advertisement

Answer

Your username is mediumclone but the database you’re connecting to is postgres, so the tables are being created under the postgres table inside of the postgres server (I know, table and server being named the same is kinda confusing). Change the DATABASE env entry to be mediumclone and you’ll get the result you want

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement