Skip to content

Changing default value with changeColumn does not work in SQL Server #7403

Closed

Description

@joshuapaling

What you are doing?

// In SQL Server...
queryInterface.changeColumn(
  'my_table', 'my_column',
  {
    type: Sequelize.INTEGER,
    defaultValue: 0,
    allowNull: false,
  }
)

What do you expect to happen?

It should successfully change the column data type and the default value.

What is actually happening?

The SQL generated does not work : ALTER TABLE [my_table] ALTER COLUMN [my_column] INTEGER NOT NULL DEFAULT 0; gives Incorrect syntax near the keyword \'DEFAULT\'

Changing a default value in SQL Server is a bit more of a pain than other RDBMSs - it basically means dropping an existing constraint (if there was a previous default value) and adding a new constraint - see http://stackoverflow.com/questions/15547210/modify-default-value-in-sql-server for more info.

Dialect: SQL Server
Database version: SQL Server for Linux
Sequelize version: Sequelize [Node: 6.3.0, CLI: 2.4.0, ORM: 3.28.0]
CLI version 3.9.1

Activity

  1. joshuapaling commented on Mar 21, 2017

    @joshuapaling
    Author

    Further to this, even just changing the type of a column without changing the default value might not work if the column has an existing default value. So even just to change the type, I'm pretty sure you need to drop the constraint which enforces the existing default value first. Perhaps not if the old and new types are compatible - eg. INT and BIG INT. This answer has some info on that: http://stackoverflow.com/a/10758357/1373987

  2. harshithkashyap commented on Mar 21, 2017

    @harshithkashyap

    This will be solved by #7108

  3. Xenope commented on Jan 29, 2019

    @Xenope

    Sorry, for my ignorance but how does this pull request have solved the original problem?
    All I see is that handling constraints have been added but it doesn't appear to be used in the changeColumn.

    Am i wrong?

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions