0

When I try to query using repository for login api, I receive this error

QueryFailedError: relation "user" does not exist

After debuging, I realize the nestjs still using database with name "postgres". eventhough I set my database name in my .env as "nestjs". (I can confirm because when I migrate and seed the database "postgres" the login function works)

DB_TYPE=postgres
DB_HOST=db
DB_USER=postgres
DB_PASS=postgres
DB_NAME=nestjs
DB_NAME_TEST=nestjs_test
DB_PORT=5432

All my migration and seed process already using database "nestjs". Below is my files to configure my database connection

configuration.ts

import { registerAs } from '@nestjs/config';

export default registerAs('database', () => ({
  type: process.env.DB_TYPE,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  name: process.env.DB_NAME,
  nameTest: process.env.DB_NAME_TEST,
  port: process.env.DB_PORT,
}));

database/config.service.ts

...
get name(): string {
    return this.configService.get<string>(
      this.configService.get<string>('app.env') === 'test'
        ? 'database.nameTest'
        : 'database.name',
    );
  }
...

database/config.migration.ts

import { SnakeNamingStrategy } from 'typeorm-naming-strategies';

export = {
  type: process.env.DB_TYPE,
  host: process.env.DB_HOST,
  port: +process.env.DB_PORT,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database:
    process.env.NODE_ENV === 'test'
      ? process.env.DB_NAME_TEST
      : process.env.DB_NAME,
  entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
  migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
  cli: {
    migrationsDir: __dirname + '/../../database/migrations',
  },
  extra: {
    charset: 'utf8mb4_unicode_ci',
  },
  synchronize: false,
  logging: true,
  keepConnectionAlive: true,
  namingStrategy: new SnakeNamingStrategy(),
};

database/provider.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { DatabaseConfigModule } from '../../config/database/config.module';
import { DatabaseConfigService } from '../../config/database/config.service';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [DatabaseConfigModule],
      inject: [DatabaseConfigService],
      useFactory: async (
        service: DatabaseConfigService,
      ): Promise<TypeOrmModuleOptions> => ({
        type: service.type,
        host: service.host,
        port: service.port,
        username: service.user,
        password: service.password,
        name: service.name,
        entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
        migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
        cli: {
          migrationsDir: __dirname + '/../../database/migrations',
        },
        extra: {
          charset: 'utf8mb4_unicode_ci',
        },
        synchronize: false,
        logging: true,
        keepConnectionAlive: true,
        namingStrategy: new SnakeNamingStrategy(),
      }),
    }),
  ],
})
export class DatabaseProviderModule {}

And here is my databases:

enter image description here

If you guys need more file for information please let me know. I'm new to nestjs and typeorm, So please tell me what's wrong with my code. Thank you!

6
  • Please delete dist folder and run your project again, and give me feedback Commented May 9, 2022 at 19:16
  • @MohammadYaserAhmadi still gives me the same error :'( Commented May 10, 2022 at 3:12
  • If you pass the config manually it works correctly? for example: name: "nestjs" Commented May 10, 2022 at 4:31
  • @MohammadYaserAhmadi no different :'( Commented May 10, 2022 at 5:37
  • can you give me the github repository to run your code on my system? Commented May 10, 2022 at 5:48

1 Answer 1

1

TypeormModule has the database property which provides the database name. You are using name in provider.module.ts:

  • Wrong name : databaseName
  • Right database : databaseName
TypeOrmModule.forRootAsync({
  useFactory: () => ({
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: 'root',
    database: 'test',
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
    synchronize: true,
  }),
});

Sign up to request clarification or add additional context in comments.

2 Comments

OMG, how can I not see this OTL. Thanks sir!
Any time sir ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.