Skip to content

queryInterface.removeColumn fails during migration #4943

Closed

Description

@apsavin

Sequelize [Node: 4.2.2, CLI: 2.2.1, ORM: 3.14.0]
mssql

module.exports = {
  up: function(queryInterface) {
    return queryInterface.removeColumn('TableName', 'ColumnName'));
  },

  down: function(queryInterface, Sequelize) {
    return queryInterface
      .addColumn('TableName', 'ColumnName', {
        type: Sequelize.STRING(100),
        field: 'ColumnName',
        allowNull: false,
        defaultValue: 'some value',
      });
  },
};

removeColumn will fail because of defaultValue constraint.
And I can't set up a name for the constraint so it's not convenient to remove the constraint with plain sql query (query becomes more complex because I need to get the constraint's name firstly)

Activity

  1. mickhansen commented on Nov 29, 2015

    @mickhansen
    Contributor

    I was not aware that default values could constrain the table in any way.
    Can you post the SQL called and the full error message?

  2. apsavin commented on Nov 29, 2015

    @apsavin
    Author

    I was not aware, too, but now I am aware) Thanks to ms.

    Sequelize error message:
    Unhandled rejection SequelizeDatabaseError: ALTER TABLE DROP COLUMN ColumnName failed because one or more objects access this column.

    sql> ALTER TABLE TableName DROP COLUMN ColumnName
    [2015-11-29 15:00:28] [S0001][5074] The object 'DF_TableName_ColumnName is dependent on column 'ColumnName'.

    Because of

    ALTER TABLE TableName
        ADD CONSTRAINT DF_TableName_ColumnName
        DEFAULT N'some value' FOR ColumnName;

    When I use queryInterface.addColumn with defaultValue behavior is the same, just the name of constraint is not so beautiful because ORM auto-generates it.

    See this discussion on social.msdn.microsoft.com. It's a common problem. Unfortunetaly, I can't find any official ms docs about it.

    Workaround: just use queryInterface.sequelize.query instead of addColumn/removeColumn methods if the column has a default value.

  3. added
    type: bug
    dialect: mssql
    on Nov 29, 2015
  4. MichaelKearnsSoftwire commented on Dec 7, 2015

    @MichaelKearnsSoftwire
    Contributor

    This is also an issue my team has been working around for a while.

    I've submitted a PR for this -- if somebody could take a look that would be useful.

  5. harshithkashyap commented on Nov 25, 2016

    @harshithkashyap

    PR #4990 has been merged. This issue should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: mssqltype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions