sequelize / sequelize/sequelize

`alter` doesn't change the default value correctly with MSSQL

Open
#14,294 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

dialect: mssql
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

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/core@7.0.0-alpha.10 (Same is happening on sequelize@6.17.0)
  • 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.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the MSSQL SQL generated by sequelize.sync({ alter: true }) for the default value on commodity_classification. Compare the emitted ALTER COLUMN statement with the expected ADD DEFAULT syntax, then verify that MSSQL alter sync updates the default without a syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.