Search code examples
postgresqlherokuknex.jsheroku-postgresnode-postgres

knex migrate throws error relation already exists


I use Heroku Postgres database and knexjs as SQL query builder in my node.js application. I try to run the latest knex migration, but I get error relation already exists. When I try to create the table by sql command CREATE TABLE, it works without any issues.

knexfile.js

// Update with your config settings.

require('dotenv').config({ path: require('find-config')('.env') });

module.exports = {
  development: {
    client: 'pg',
    useNullAsDefault: true,
    connection: process.env.DATABASE_URL,
    searchPath: ['knex', 'public'],
  },

  staging: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user: 'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  },

  production: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user: 'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }
};
  • the latest migration file

migrations/20180120183247_users_test.js

/* eslint func-names: 0 */

exports.up = function (knex) {
  return knex.schema
    .createTable('users', (table) => {
      table.increments('id').primary();
      table.string('user_name', 100);
      table.text('password');
      table.string('email', 254);
    });
};

exports.down = function (knex) {
  return knex.schema
    .dropTableIfExists('users');
};

When I run command:

knex migrate:latest --env development

I get error:

Using environment: development
Knex:warning - migration file "20180120184707_initial_schema.js" failed
Knex:warning - migration failed with error: create table "users" ("id" serial primary key, "user_name" varchar(100), "password" text, "email" varchar(254)) - relation "users" already exists
error: relation "users" already exists
    at Connection.parseE (.../node_modules/pg/lib/connection.js:546:11)
    at Connection.parseMessage (.../node_modules/pg/lib/connection.js:371:19)
    at TLSSocket.<anonymous> (.../node_modules/pg/lib/connection.js:114:22)
    at emitOne (events.js:115:13)
    at TLSSocket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at TLSSocket.Readable.push (_stream_readable.js:197:10)
    at TLSWrap.onread (net.js:589:20)

If I connect to database and run command:

select * from users;

I get:

ERROR:  relation "users" does not exist
LINE 1: select * from users;

In Heroku Postgres settings I run Reset Database to delete all data. I waited for 2-3 minutes and then tried to run the latest migration again, but I got the same error message.


Solution

  • I didn't know knex migrate:latest command runs all migration files so the table users was created in previous migration file and that was the issue.