Reputation: 471
I know that this question is already answered many times but i can't figured out what to do. I'm using Postgres 14.7 ,NestJs and TypeOrm in my project. I've created many entities, but for debugging purposes I use only one. Here is my Entity:
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn, Unique } from 'typeorm';
import { Supply } from './supply.entity';
@Entity({ name: 'Printers' })
@Unique(['model', 'manufacturer'])
export class Printer {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'varchar' })
model: string;
@Column({ type: 'varchar' })
manufacturer: string;
@Column({ type: 'varchar', nullable: true })
description: string;
@Column({ type: 'boolean', nullable: true })
hasWifi: boolean;
@Column({ type: 'boolean', nullable: true })
hasUsb: boolean;
@Column({ type: 'boolean', nullable: true })
hasEthernet: boolean;
@Column({ type: 'boolean', nullable: true })
hasScanner: boolean;
@Column({ type: 'boolean', nullable: true })
hasFax: boolean;
@Column({ type: 'boolean', nullable: true })
hasUsbFlashDriver: boolean;
@Column({ type: 'varchar', nullable: true })
image: string;
@Column({ type: 'double precision', nullable: true })
price: number;
//
// @ManyToMany(() => Supply,supply => supply.printers)
// supplies: Supply[];
}
Also, I use the Repository Pattern explained in here: NestJs TypeOrm Repository Pattern
Here is my dataSource object.
import { DataSource } from 'typeorm';
import * as Entities from '../../Entities';
import { Printer } from '../../Entities';
export const postgresProvider = [{
provide: 'DATA_SOURCE',
useFactory: async () => {
const dataSource = new DataSource({
database: process.env.DB_NAME,
type: 'postgres',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
ssl: false,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
entities: [Printer],
synchronize: true,
logging: true,
});
return await dataSource.initialize();
},
}];
As you can observe , I'm only importing the Printer Entity. So only the Printer Entity will be created.
The error I'm getting every time:
[Nest] 7772 - 02/24/2023, 8:57:49 AM LOG [InstanceLoader] DatabaseModule dependencies initialized +213ms [Nest] 7772 - 02/24/2023, 8:57:49 AM LOG [InstanceLoader] ActionModule dependencies initialized +2ms query failed: CREATE TABLE "Printers" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "model" character varying NOT NULL, "manufacturer" character varying NOT NULL, "description" character varying, "hasWifi" boolean, "hasUsb" boolean, "hasEthernet" boolean, "hasScanner" boolean, "hasFax" boolean, "hasUsbFlashDriver" boolean, "image" character varying, "price" double precision, CONSTRAINT "UQ_8457ad379d3c9921d912c31f169" UNIQUE ("model", "manufacturer"), CONSTRAINT "PK_882f70100101eba7cdd16e42e52" PRIMARY KEY ("id")) error: error: duplicate key value violates unique constraint "pg_type_typname_nsp_index" query: ROLLBACK [Nest] 7772 - 02/24/2023, 8:57:49 AM ERROR [ExceptionHandler] duplicate key value violates unique constraint "pg_type_typname_nsp_index" QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index" at PostgresQueryRunner.query (C:\Users\Manager\Desktop\Workspace\src\driver\postgres\PostgresQueryRunner.ts:299:19) at processTicksAndRejections (node:internal/process/task_queues:95:5) at PostgresQueryRunner.executeQueries (C:\Users\Manager\Desktop\Workspace\src\query-runner\BaseQueryRunner.ts:651:13) at PostgresQueryRunner.createTable (C:\Users\Manager\Desktop\Workspace\src\driver\postgres\PostgresQueryRunner.ts:571:9) at RdbmsSchemaBuilder.createNewTables (C:\Users\Manager\Desktop\Workspace\src\schema-builder\RdbmsSchemaBuilder.ts:613:13) at RdbmsSchemaBuilder.executeSchemaSyncOperationsInProperOrder (C:\Users\Manager\Desktop\Workspace\src\schema-builder\RdbmsSchemaBuilder.ts:224:9) at RdbmsSchemaBuilder.build (C:\Users\Manager\Desktop\Workspace\src\schema-builder\RdbmsSchemaBuilder.ts:95:13) at DataSource.synchronize (C:\Users\Manager\Desktop\Workspace\src\data-source\DataSource.ts:329:9) at DataSource.initialize (C:\Users\Manager\Desktop\Workspace\src\data-source\DataSource.ts:267:43) at InstanceWrapper.useFactory [as metatype] (C:\Users\Manager\Desktop\Workspace\src\Endpoints\Database\data-source.ts:20:10)
Many people say that this error occurs because of the uuidv4 libary of the postgres. Other say that (if i understood well enough) that postgres creates two tables . One for testing purposes and one for the development. I've even commented out all the fields in the printer entity and i've let only the model field. Same error.
What I'm doing wrong? Thank you in advance.
Upvotes: 2
Views: 1953
Reputation: 131
Dumb question - Are you certain the new "inserts" don't have the same model/manufacturer as existing or previous rows? The following would cause the "duplicate key value violates unique constraint" error.
@Unique(['model', 'manufacturer'])
Try removing that decoration to determine if that solves the duplicate issue. If so, the problem is with the data.
Upvotes: 0