Skip to content

alter doesn't change the default value correctly with MSSQL #14294

Open

Description

@timhaak

Issue Creation Checklist

Bug Description

const { Model } = require('@sequelize/core');
module.exports = (sequelize, DataTypes) => {
  class SmsAccounts extends Model {
    static associate(models) {
    }
  }

  SmsAccounts.init(
    {
      expiresOn: {
        type: DataTypes.DATE,
        allowNull: false,
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      portalId: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      commodityClassification: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      parentId: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
    },
    {
      sequelize,
      tableName: 'accounts',
      schema: process.env.SMS_SCHEMA,
      underscored: true,
    },
  );
  return SmsAccounts;
};
await sequelize.sync({ alter: true });

What do you expect to happen?

Update/create a table in mssql setting default values

What is actually happening?

Failing to set default value due to incorrect syntax.

Executing (default): ALTER TABLE [sms_test].[accounts] ALTER COLUMN [commodity_classification] BIT NOT NULL DEFAULT 0;
✖ Incorrect syntax near the keyword 'DEFAULT'.
✖ Error

Additional context

It should be doing something similar to the following

ALTER TABLE [sms_test].[accounts] ADD DEFAULT 0 FOR commodity_classification;

Environment

  • Sequelize version: @sequelize/[email protected] (Same is happening on [email protected])
  • Node.js version: v17.8.0
  • If TypeScript related: TypeScript version: N/A
  • Database & Version: MSSQL (Microsoft SQL Azure (RTM) - 12.0.2000.8 )
  • Connector library & Version: tedious (14.4.0)

Would you be willing to resolve this issue by submitting a Pull Request?

  • [x ] No, I don't have the time, although I believe I could do it if I had the time...
    (Will try possibly next month once I've put out some fires.)

Activity

  1. added
    type: bug
    dialect: mssql
    on Mar 30, 2022
  2. changed the title [-]MSSQL- set default value[/-] [+]`alter` doesn't change the default value correctly with MSSQL[/+] on Mar 30, 2022
  3. ephys commented on Mar 30, 2022

    @ephys
    Member

    I've taken the liberty of updating the title for easier search queries
    I'll also link this closed but unresolved issue to redirect users here if they find the other one #12797

    Unfortunately sync({ alter }) has a large number of bugs, but fixing them is low on our priority list.
    If you do end up having the time to resolve this issue, a PR would be fantastic :)

  4. timhaak commented on Mar 30, 2022

    @timhaak
    Author

    Thanks for the reply :)

    Not a problem. I'm most likely going to try to solve this by just doing migrations for now :).

    More that the bug is logged and if I do find some free time I'll try to sort it out.

    Though from the quick look into MSSQL default think it's going to be tougher than it should.

    Thanks for the library and work put into it :).

  5. added theissue type on Mar 9, 2025
  6. removed
    type: bug
    on Mar 9, 2025
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: mssql

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions