typeorm: [PROBLEM] constraint "PRIMARY KEY" of relation "TABLE" does not exist

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [x] @next [ ] 0.x.x (or put your version here)

Question I want to use repository to find my sample data in postgres but i’m stuck !!

Example Files User.ts

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  userid: number;

  @Column()
  usermail: string;
}

My Problem { QueryFailedError: constraint “PK_04d500b286ce3e4c4cbc20d97ea” of relation “user” does not exist at new QueryFailedError (C:\Users\develop\Desktop\TypeORM\src\error\QueryFailedError.ts:7:9) at Query.callback (C:\Users\develop\Desktop\TypeORM\src\driver\postgres\PostgresQueryRunner.ts:175:30) at Query.handleError (C:\Users\develop\Desktop\TypeORM\node_modules\pg\lib\query.js:142:17) at Connection.connectedErrorMessageHandler (C:\Users\develop\Desktop\TypeORM\node_modules\pg\lib\client.js:160:17) at Connection.emit (events.js:182:13) at Connection.EventEmitter.emit (domain.js:442:20) at Socket.<anonymous> (C:\Users\develop\Desktop\TypeORM\node_modules\pg\lib\connection.js:125:12) at Socket.emit (events.js:182:13) at Socket.EventEmitter.emit (domain.js:442:20) at addChunk (_stream_readable.js:283:12) message: ‘constraint “PK_04d500b286ce3e4c4cbc20d97ea” of relation “app” does not exist’,

I think this is PROBLEM ALTER TABLE “user” DROP CONSTRAINT “PK_04d500b286ce3e4c4cbc20d97ea”

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 19 (4 by maintainers)

Most upvoted comments

Same here as @ZoroSC stated. Disabling synchronize option resolved the issue

Versions: TypeORM: 0.2.17 pg: 9.5.16

Sure but you wrote about ALTER TABLE "user" DROP CONSTRAINT "PK_04d500b286ce3e4c4cbc20d97ea" and I really dont know where this comming from because find method do not generate such SQL.

Could you please create small reproduction of this problem? Maybe small repository with code or snipped of code with entity definitions?

Its coming from migrations @vlapo . We need to improvise the queries generated via migrations. Just need to add extra bit of checking drop table / constraints if exists

I’m getting same error while starting my project using command “yarn start:debug” (using nestjs). I’m having a many-to-many relation between products and categories. My product entity is as follows: @ManyToMany(type => Category) @JoinTable({ name: 'products_categories', joinColumn: { name: 'productId' }, inverseJoinColumn: { name: 'categoryId' }, }) category: Category[];

Disabling synchronize isn’t an appropriate solution. I don’t understand why is it trying to drop constraint and even if it does then it should use “if exists”

@pleerock @MrJim-06 @Kononnable

@Kononnable DROP CONSTRAINT IF EXISTS instead of just DROP CONSTRAINT is one possible solution to this, only for pg 9.0+.

I’m also having an issue with this same thing, and it seems to be throwing cause I’m using the synchronize option as seen here.

connectionManager.create({
	name: 'postgres',
	type: 'postgres',
	port: 5432,
	username: 'postgres',
	password: 'password',
        database: 'postgres',
	entities: [GuildSettings],
	synchronize: true
});

With my one entity looking like this:

import {
   Entity,
   Column,
   PrimaryColumn
} from 'typeorm';

@Entity('settings')
export class GuildSettings {

   @PrimaryColumn()
   guild: string;

   @Column({ type: 'jsonb', default: "'{}'"})
   settings: any;
}

Versions: TypeORM: 0.2.16 pg: 7.9.0

I think I’ve met similar or same issue. In my project I have NestJS + Typeorm + Postgres and bunch of migrations. One of these migrations creates a table with a custom PK constraint name:

create table my_table(id char(26) not null constraint my_table_pk primary key, json json, hash char(40));

So, if I run migrations and then nest start --watch the app I get an error:

[Nest] 40830   - 01/19/2021, 11:51:50 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +106ms
QueryFailedError: constraint "PK_5f3350bd9b6c92a62a2257730b7" of relation "my_table" does not exist
    at new QueryFailedError (myproject/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at Query.callback (myproject/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
    at Query.handleError (myproject/node_modules/pg/lib/query.js:145:17)
    at Connection.connectedErrorMessageHandler (myproject/node_modules/pg/lib/client.js:214:17)
    at Connection.emit (events.js:314:20)
    at Connection.EventEmitter.emit (domain.js:483:12)
    at Socket.<anonymous> (myproject/node_modules/pg/lib/connection.js:134:12)
    at Socket.emit (events.js:314:20)
    at Socket.EventEmitter.emit (domain.js:483:12)
    at addChunk (_stream_readable.js:297:12)

and this constraint definitely does not exist, because this constraint has custom name my_table_pk in migration files. For some unknown reasons typeorm (?) tries to use another name for the constraint.

If I just nest start my application with fresh empty DB, it starts without errors and I see DB with tables and so on, although I didn’t run migrations. Moreover, the custom constraint in that case has name PK_5f3350bd9b6c92a62a2257730b7 which is exactly as it was in the error. But that case is not appropriate for me, because migration with custom name is already in prod, so I can’t reverse it, and because there are some other differences with requirements (e.g. wrong column type for other column)

Any ideas how it can be fixed? It works with synchronize false, but it seems to me like a just workaround, isn’t it?