Receiving "QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index" error on TypeORM

Viewed 3750

I've started a project with TypeORM and I'm having a problem. I've got the synchronize option set to true on TypeORM config, but when I run the application I receive the following error: QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index"

I've read a little bit about it but the only thing I found was someone saying that it was something related to running tests in parallel, not the same issue I've got.

It could be something related to async/await maybe, here is the database connection part:

export const databaseProviders = [
    {
        provide: 'DATABASE_CONNECTION',
        useFactory: async () => await createConnection({
            type: "postgres",
            host: process.env.TYPEORM_HOST.toString(),
            port: Number(process.env.PORT),
            username: process.env.TYPEORM_USER.toString(),
            password: process.env.TYPEORM_PASSWORD.toString(),
            database: process.env.TYPEORM_DATABASE,
            entities: [
                 __dirname + '/../**/*.entity{.ts,.js}',
            ],
            synchronize: true,
            logging: true
        })
    }
]

I don't intend to use this project in production, I want TypeORM to generate the table for me, that's it. Maybe I could solve this by setting synchronize to false and using migrations.

It seems to be creating, or trying to create the table 3 times.

TypeORM logging

Anyway, hope someone can help me.

2 Answers

I have the same problem.

I was defining the same index twice.

  @Index({ unique: true })
  @Column({ type: 'varchar', nullable: true, unique: true })

I had the same problem, it was resolved by deleting the dist folder. The reason this can be an issue is because of previous migrations.

Related