Skip to content

Alter queries generated by sequelize.sync with alter: true report syntax problems when columns use an explicit defaultValue #9058

Closed

Description

@jsanta

What are you doing?

Syncing using alter: true for a table model using a column with an explicit definiton for the defaultValue.

// Model
const User = sequelize.define('User',
    {
        id: {
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        },
        email: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: 'usr_email',
            set(val) {
                const email = (!val) ? this.getDataValue('username') : val;
                this.setDataValue('email', email.toLowerCase());
            },
            validate: {
                isEmail: true
            }
        },
        isValidated: {
            type: Sequelize.BOOLEAN,
            defaultValue: false
        }
    },
    {
        timestamps: true,
        freezeTableName:true,
        paranoid: true
    }
);

exports.User = User;
// DB Sync
var sequelize = db.sequelize;
    sequelize.sync({
        force: false,
        logging: dbLog,
        alter: true
    }).then(function () { /* ... */ });

What do you expect to happen?

Alter queries executed without any syntax error

What is actually happening?

Alter queries built with an invalid syntax.

Output messages

{
  SequelizeDatabaseError: Incorrect syntax near the keyword 'DEFAULT'. [...] ,
  sql: 'ALTER TABLE [User] ALTER COLUMN [isValidated] BIT DEFAULT 0;'
}

Workaround

Modify the model definition to use a function that returns the default value the column instead of specifying the value itself:

// Model
const User = sequelize.define('User',
    {
        id: {
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
            allowNull: false
        },
        email: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: 'usr_email',
            set(val) {
                const email = (!val) ? this.getDataValue('username') : val;
                this.setDataValue('email', email.toLowerCase());
            },
            validate: {
                isEmail: true
            }
        },
        isValidated: {
            type: Sequelize.BOOLEAN,
            defaultValue: () => false
        }
    },
    {
        timestamps: true,
        freezeTableName:true,
        paranoid: true
    }
);

exports.User = User;

Dialect: mssql
Dialect version: 4.29.2
Database version: MS Sql 2017 running on Ubuntu Linux
Sequelize version: 4.29.2
Tested with latest release: No

Activity

  1. sushantdhiman commented on Feb 16, 2018

    @sushantdhiman
    Contributor

    Can you try with latest version

  2. stale commented on May 17, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  3. jsanta commented on May 22, 2018

    @jsanta
    Author

    Tried the latest version.
    Still getting the same error. MS SQL Server syntax for altering a column default value is different. Refer to https://stackoverflow.com/questions/6791675/how-to-set-a-default-value-for-an-existing-column

  4. sushantdhiman commented on May 23, 2018

    @sushantdhiman
    Contributor
  5. luisotavio-dev commented on Jun 24, 2022

    @luisotavio-dev

    Unfortunately I'm facing the same problem currently. SQL Server 2019. Apparently the syntax for setting a DEFAULT value to an existing column is different in SQL SERVER, causing this exception, exactly as explained by user @jsanta :(
    Any solution? I had to disable sync() to be able to work with my API.

  6. pn-andrej-zecevic commented on May 30, 2024

    @pn-andrej-zecevic

    I am still having this issue in 6.37.3 over 4 years later. The workaround of using a function instead of a literal value when using changeColumn worked for me. When using addColumn it worked better with a literal value.

  7. kieran-reportabl commented on Jun 25, 2024

    @kieran-reportabl

    Still an issue. Cannot migrate modified models with a default value column.

    sequelize: 6.37.3
    tedious: 18.2.0

    ...
    Executing (bdb79bb2ea41f506432f): ALTER TABLE [Users] ALTER COLUMN [status] NVARCHAR(10) NOT NULL DEFAULT N'ACTIVE';
    Executing (bdb79bb2ea41f506432f): ROLLBACK TRANSACTION;
    
    ERROR: SequelizeDatabaseError: Incorrect syntax near the keyword 'DEFAULT'.
        at Query.run (...\node_modules\sequelize\lib\dialects\mssql\query.js:107:25)
        at ...\node_modules\sequelize\lib\sequelize.js:315:28
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions