kimjbstar/sequelize-typescript-migration

[SequelizeDatabaseError]: relation "sequelizemeta" does not exist

Pietrox opened this issue · 6 comments

Hello!
First of all thanks for taking your time into trying to solve a big pain for many developers in this manner ;)

I wanted to ask on how to implement it with sequelize v5 and typescript set to esm2017.

I had to modify the code of makemigration.ts into this:

import { SequelizeTypescriptMigration } from 'sequelize-typescript-migration';
import * as path from 'path';

require('dotenv').config();

const sequelize: Sequelize = new Sequelize({
  dialect: 'postgres',
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  host: process.env.DB_HOST,
  modelMatch: (filename, member) => {
    return filename.substring(0, filename.indexOf('.model.ts')) === member.toLowerCase();
  }

});


// @ts-ignore
async function migrate() {
  try {
    await SequelizeTypescriptMigration.makeMigration(sequelize, {
      outDir: path.join(__dirname, './migrations'),
      migrationName: 'migration',
      preview: true
    });
  } catch (e) {
    console.log(e);
    return e;
  }
}

migrate().then(r => {
  return r;
});

Currently I'm getting this error:

Executing (default): CREATE TABLE IF NOT EXISTS "SequelizeMeta" ("name" VARCHAR(255) NOT NULL UNIQUE , PRIMARY KEY ("name"));
Executing (default): CREATE TABLE IF NOT EXISTS "SequelizeMetaMigrations" ("revision" INTEGER NOT NULL UNIQUE , "name" VARCHAR(255) NOT NULL, "state" JSON NOT NULL, PRIMARY KEY ("revision"));
Executing (default): SELECT name FROM SequelizeMeta ORDER BY name desc limit 1
DatabaseError [SequelizeDatabaseError]: relation "sequelizemeta" does not exist
    at Query.formatError (E:\Projects\100bet\node_modules\sequelize\lib\dialects\postgres\query.js:366:16)
    at E:\Projects\100bet\node_modules\sequelize\lib\dialects\postgres\query.js:72:18
    at tryCatcher (E:\Projects\100bet\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (E:\Projects\100bet\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (E:\Projects\100bet\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (E:\Projects\100bet\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (E:\Projects\100bet\node_modules\bluebird\js\release\promise.js:725:18)
    at _drainQueueStep (E:\Projects\100bet\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (E:\Projects\100bet\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (E:\Projects\100bet\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (E:\Projects\100bet\node_modules\bluebird\js\release\async.js:15:14)
    at processImmediate (internal/timers.js:458:21) {
  parent: error: relation "sequelizemeta" does not exist
      at Parser.parseErrorMessage (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:357:11)
      at Parser.handlePacket (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:186:21)
      at Parser.parse (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:101:30)
      at Socket.<anonymous> (E:\Projects\100bet\node_modules\pg-protocol\src\index.ts:7:48)
      at Socket.emit (events.js:314:20)
      at Socket.EventEmitter.emit (domain.js:486:12)
      at addChunk (_stream_readable.js:303:12)
      at readableAddChunk (_stream_readable.js:279:9)
      at Socket.Readable.push (_stream_readable.js:218:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
    length: 112,
    severity: 'ERROR',
    code: '42P01',
    detail: undefined,
    hint: undefined,
    position: '18',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'parse_relation.c',
    line: '1191',
    routine: 'parserOpenTable',
    sql: 'SELECT name FROM SequelizeMeta ORDER BY name desc limit 1',
    parameters: undefined
  },
  original: error: relation "sequelizemeta" does not exist
      at Parser.parseErrorMessage (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:357:11)
      at Parser.handlePacket (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:186:21)
      at Parser.parse (E:\Projects\100bet\node_modules\pg-protocol\src\parser.ts:101:30)
      at Socket.<anonymous> (E:\Projects\100bet\node_modules\pg-protocol\src\index.ts:7:48)
      at Socket.emit (events.js:314:20)
      at Socket.EventEmitter.emit (domain.js:486:12)
      at addChunk (_stream_readable.js:303:12)
      at readableAddChunk (_stream_readable.js:279:9)
      at Socket.Readable.push (_stream_readable.js:218:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
    length: 112,
    severity: 'ERROR',
    code: '42P01',
    detail: undefined,
    hint: undefined,
    position: '18',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'parse_relation.c',
    line: '1191',
    routine: 'parserOpenTable',
    sql: 'SELECT name FROM SequelizeMeta ORDER BY name desc limit 1',
    parameters: undefined
  },
  sql: 'SELECT name FROM SequelizeMeta ORDER BY name desc limit 1',
  parameters: undefined
}

Do you think that this error is related to the code I made or maybe library compatibility?

I created 2 views in database:

CREATE VIEW SequelizeMeta AS SELECT * FROM "SequelizeMeta";
CREATE VIEW SequelizeMetaMigrations AS SELECT * FROM "SequelizeMetaMigrations";

it`s clumsy, but works

If the table name can be case insensitive, you can try to enable quoteIdentifiers: false, Sequelize will not create tables with quotes.

It's worked for me. For example:

const sequelize: Sequelize = new Sequelize(
  process.env.POSTGRES_DB || '',
  process.env.POSTGRES_USER_NAME || '',
  process.env.POSTGRES_PASSWORD || '',
  {
    dialect: 'postgres',
    port: 5432,
    database: process.env.POSTGRES_DB,
    host: process.env.HOST || '',
    pool: {
      max: 20,
      min: 0,
      acquire: 30000,
      idle: 10000,
    },
    quoteIdentifiers: false,
    logging: true,
    models: [__dirname + '/src/models'],
  },
);

If the table name can be case insensitive, you can try to enable quoteIdentifiers: false, Sequelize will not create tables with quotes.

It's worked for me. For example:

const sequelize: Sequelize = new Sequelize(
  process.env.POSTGRES_DB || '',
  process.env.POSTGRES_USER_NAME || '',
  process.env.POSTGRES_PASSWORD || '',
  {
    dialect: 'postgres',
    port: 5432,
    database: process.env.POSTGRES_DB,
    host: process.env.HOST || '',
    pool: {
      max: 20,
      min: 0,
      acquire: 30000,
      idle: 10000,
    },
    quoteIdentifiers: false,
    logging: true,
    models: [__dirname + '/src/models'],
  },
);

it works but make problem in camel case column in models!

I fork project and fix the problem . use this package instead.
npm uninstall sequelize-typescript-migration
npm i sequelize-typescript-migration-fix
and change your import to
import { SequelizeTypescriptMigration } from "sequelize-typescript-migration-fix";

For me it was resolved by adding searchPath to my config:

  "development": {
    "url": "postgres://postgres:wouldntyouliketoknow@db:5432/postgres",
    "dialect": "postgres",
    "dialectOptions": {
      "searchPath": "public",
      "prependSearchPath": true
    }
  },