Migrations not working for me with "QueryFailedError: relation "posts" does not exist
See original GitHub issueEverytime I drop schema and try to run migration, it ends up with this
$ ts-node ./node_modules/typeorm/cli.js migration:run
query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
query: CREATE TABLE "migrations" ("id" SERIAL NOT NULL, "timestamp" bigint NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY ("id"))
query: SELECT * FROM "migrations" "migrations"
0 migrations are already loaded in the database.
3 migrations were found in the source code.
3 migrations are new migrations that needs to be executed.
query: START TRANSACTION
query: CREATE TABLE "votes" ("id" SERIAL NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "value" integer NOT NULL, "username" character varying NOT NULL, "postId" integer, "commentId" integer, CONSTRAINT "PK_f3d9fd4a0af865152c3f59db8ff" PRIMARY KEY ("id"))
query: ALTER TABLE "posts" DROP CONSTRAINT "FK_42377e3f89a203ca74d117e5961"
query failed: ALTER TABLE "posts" DROP CONSTRAINT "FK_42377e3f89a203ca74d117e5961"
error: error: relation "posts" does not exist
at Parser.parseErrorMessage (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:486:12)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:284:9)
at Socket.Readable.push (_stream_readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
length: 103,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'namespace.c',
line: '426',
routine: 'RangeVarGetRelidExtended'
}
query: ROLLBACK
Error during migration run:
QueryFailedError: relation "posts" does not exist
at new QueryFailedError (/Users/piyushmehta/social-shout/server/src/error/QueryFailedError.ts:9:9)
at Query.callback (/Users/piyushmehta/social-shout/server/src/driver/postgres/PostgresQueryRunner.ts:176:30)
at Query.handleError (/Users/piyushmehta/social-shout/server/node_modules/pg/lib/query.js:128:19)
at Client._handleErrorMessage (/Users/piyushmehta/social-shout/server/node_modules/pg/lib/client.js:335:17)
at Connection.emit (events.js:315:20)
at Connection.EventEmitter.emit (domain.js:486:12)
at /Users/piyushmehta/social-shout/server/node_modules/pg/lib/connection.js:115:12
at Parser.parse (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/parser.ts:102:9)
at Socket.<anonymous> (/Users/piyushmehta/social-shout/server/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20) {
length: 103,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'namespace.c',
line: '426',
routine: 'RangeVarGetRelidExtended',
query: 'ALTER TABLE "posts" DROP CONSTRAINT "FK_42377e3f89a203ca74d117e5961"',
parameters: []
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
node.js - TypeORM: QueryFailedError: relation does not exist
I put the data source [countries] in a JSON file and this work for me: ` // In a prev migration the countries...
Read more >typeorm/typeorm - Gitter
Help, for some reason i give the connection a User class but whe asking for the Repository i get error: QueryFailedError: relation "user"...
Read more >constraint "PK_b380f7af92fddcafe76bdf2c652" of relation ...
The error message tells you what is wrong. Your migrations (assuming since you give no information but some transpiled js) try to drop...
Read more >Postgres : Relation does not exist error
But when I try to fire a select * query, it gave me this error: dump=> select * from Approvals; ERROR: relation "approvals"...
Read more >Migrations - typeorm - GitBook
ALTER TABLE "post" ALTER COLUMN "title" RENAME TO "name";. Once you run this SQL query your database schema is ready to work with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I came across the same issue and this is how I resolved it.
"synchronize": true,
inorm.config.js
filenpm start
to populate your database.synchronize
and continue withmigration
[optional]hope this helps. cheers!
Remember, in your migration files if you’re using the queryRunner and manager, you’re using the CURRENT, LATEST schema defined in your code, so all existing relations, models, etc in code will be used to run migration code.
So if in one migration you deleted a column or relation, and in the next migration your queryRunner tries to eagerly fetch data from a relation it sees in code but no longer exists in the database, it will trigger these types of errors.
The possible way to avoid this is to use raw SQL or query builder where you have more control over your migration file.