typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

Home Page:http://typeorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migrations not working for me with "QueryFailedError: relation "posts" does not exist

piyush97 opened this issue · comments

Everytime 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.

Migrations are meant to update your existing schema, it can't work if you have dropped your existing schema.

I came across the same issue and this is how I resolved it.

  1. remove the migrations folder
  2. set "synchronize": true, in orm.config.js file
  3. npm start to populate your database.
  4. after you can disable synchronize and continue with migration[optional]

hope this helps. cheers!

Works like a charm @benyam7

I came across the same issue and this is how I resolved it.

  1. remove the migrations folder
  2. set "synchronize": true, in orm.config.js file
  3. npm start to populate your database.
  4. after you can disable synchronize and continue with migration[optional]

hope this helps. cheers!

Thanks so much. God bless you

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.