Cli: migration.removeColumn - Getting a check that column/key exists error.

Created on 13 Jun 2016  路  6Comments  路  Source: sequelize/cli

Hi all,

I'm getting an error when undoing a migration I just ran to add a column, the cli does not recognize the existence of the column I just created. Here are my migrations:

1-create-user

module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      email: {
        type: Sequelize.STRING,
        unique: true
      }, createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      }, updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('Users');
  }
};

2-add-test-column

module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.addColumn(
      'Users',
      'test_column',
      {
        type: Sequelize.STRING,
        unique: true
      }
    );
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.removeColumn('Users', 'test_column');
  }
};

Steps to reproduce:
sequelize db:migrate
sequelize db:migrate:undo

Error:

Unhandled rejection SequelizeDatabaseError: ER_CANT_DROP_FIELD_OR_KEY: Can't DROP 'test_column'; check that column/key exists
    at Query.formatError (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\sequelize\lib\dialects\mysql\query.js:175:14)
    at Query._callback (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\sequelize\lib\dialects\mysql\query.js:49:21)
    at Query.Sequence.end (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\protocol\sequences\Sequence.js:85:24)
    at Query.ErrorPacket (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\protocol\sequences\Query.js:94:8)
    at Protocol._parsePacket (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\protocol\Protocol.js:280:23)
    at Parser.write (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\protocol\Parser.js:74:12)
    at Protocol.write (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (C:\Users\Leon\Desktop\RGG Web\soda-api\node_modules\mysql\lib\Connection.js:109:28)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:534:20)

Most helpful comment

I solved this by doing two migrations:
1) return queryInterface.removeIndex('users', 'username')
2) return queryInterface.removeColumn('users', 'username')

Somehow this worked better then @ragingnerd 's solution. No errors were thrown

All 6 comments

This is a bug with how sequelize tries to remove a non-existent foreign key constraint when there is an index on the column. I'm going to close this issue.

For anyone looking for a workaround, remove your indexes before removing the column.

2-add-test-column

return queryInterface
      .removeIndex('Users', 'test_column')
      .then(queryInterface.removeColumn('Users', 'test_column'));

@ragingnerd Please reopen it, this is a bug.

@myaskevich but not in the CLI

I solved this by doing two migrations:
1) return queryInterface.removeIndex('users', 'username')
2) return queryInterface.removeColumn('users', 'username')

Somehow this worked better then @ragingnerd 's solution. No errors were thrown

This seeps to stipp be a bug @ragingnerd, can we reopen it?

 return queryInterface.addColumn(TABLE, COL, {
        unique: true,
        type: Sequelize.STRING,
        allowNull: true
      });

that created a column. Then I cannot remove it with

queryInterface.removeColumn(TABLE, COLUMN)

That seems like a bug to me.

@alockwood05 It is a bug in sequelize, not the cli. This repo is for the cli.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LoneWolfPR picture LoneWolfPR  路  5Comments

papb picture papb  路  5Comments

eharoldreyes picture eharoldreyes  路  3Comments

przbadu picture przbadu  路  3Comments

f1nnix picture f1nnix  路  4Comments