Skip to content

.sync({alter:true)} causes issue with Boolean Default Values in MSSQL #10101

Closed

Description

@Justintlai

What are you doing?

Creating a model with a boolean that has a defaultValue: false in MSSQL

isAdmin: { type: DataTypes.BOOLEAN, defaultValue: false, allowNull: false }

What do you expect to happen?

When running the app with alter: true it should automatically update and alter tables requiring updates.

What is actually happening?

The line that it errors on is relating to the boolean field with a default value.

Executing (default): ALTER TABLE [solution_managers] ALTER COLUMN [isAdmin] BIT NOT NULL DEFAULT 0;

The error given by sequelize:

Unhandled rejection SequelizeDatabaseError: Incorrect syntax near the keyword 'DEFAULT'.
    at Query.formatError (\node_modules\sequelize\lib\dialects\mssql\query.js:315:12)
    at Request.connection.lib.Request [as userCallback] (\node_modules\sequelize\lib\dialects\mssql\query.js:107:25)
    at Request.callback (\node_modules\tedious\lib\request.js:37:27)
    at Connection.endOfMessageMarkerReceived (\node_modules\tedious\lib\connection.js:1880:20)
    at Connection.dispatchEvent (\node_modules\tedious\lib\connection.js:997:36)    at Parser.tokenStreamParser.on (\node_modules\tedious\lib\connection.js:827:14)
    at Parser.emit (events.js:182:13)
    at Parser.parser.on.token (\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
    at Parser.emit (events.js:182:13)
    at addChunk (\node_modules\readable-stream\lib\_stream_readable.js:301:12)
    at readableAddChunk (\node_modules\readable-stream\lib\_stream_readable.js:283:11)
    at Parser.Readable.push (\node_modules\readable-stream\lib\_stream_readable.js:247:10)
    at Parser.Transform.push (\node_modules\readable-stream\lib\_stream_transform.js:149:32)
    at Parser.afterTransform (\node_modules\readable-stream\lib\_stream_transform.js:92:10)
    at Parser._transform (\node_modules\tedious\lib\token\stream-parser.js:40:7)    at Parser.Transform._read (\node_modules\readable-stream\lib\_stream_transform.js:185:10)
    at Parser.Transform._write (\node_modules\readable-stream\lib\_stream_transform.js:173:83)
    at doWrite (\node_modules\readable-stream\lib\_stream_writable.js:429:139)
    at writeOrBuffer (\node_modules\readable-stream\lib\_stream_writable.js:418:5)
    at Parser.Writable.write (\node_modules\readable-stream\lib\_stream_writable.js:327:11)
    at Parser.addEndOfMessageMarker (\node_modules\tedious\lib\token\token-stream-parser.js:45:24)
    at Connection.message (\node_modules\tedious\lib\connection.js:1869:32)

Dialect: mssql
Dialect version: XXX
Database version: SQL SERVER 2016
Sequelize version: 4.41.0
Tested with latest release: 5.0.0-beta.13

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. lyh2668 commented on Nov 16, 2018

    @lyh2668

    I found the same problem as you.

  2. sushantdhiman commented on Dec 1, 2018

    @sushantdhiman
    Contributor
  3. added
    dialect: mssql
    on Dec 1, 2018
  4. stale commented on Mar 1, 2019

    @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 🙂

  5. Taher62 commented on Aug 2, 2019

    @Taher62

    Is this issue solved ?
    I have the same problem

  6. papb commented on Aug 2, 2019

    @papb
    Member

    @Taher62 Please open a new bug report and fill the template.

  7. added
    dialect: mssql
    type: bug
    and removed
    dialect: mssql
    on Aug 2, 2019
  8. dioncrs commented on Jul 23, 2020

    @dioncrs

    I encountered this problem here too, it occurs because he tries to change the default value of the column with "ALTER COLUMN" in my experience with MSSQL this is impossible to do this way. In MSSQL whenever a table with a default value is created, it adds a constraint for that value. I know it's a lot of work, so here where I work we never change the default value, my suggestion for developers is to remove the update from the default value of the query when using connection.sync ({alter: true}).

    Useful link: https://stackoverflow.com/questions/15547210/modify-default-value-in-sql-server

  9. Angrigo commented on Mar 15, 2021

    @Angrigo

    still an issue, any news?
    "sequelize": "^6.5.0",
    "sequelize-cli": "^6.2.0",
    "tedious": "^9.2.3",

  10. BevanLandoByte commented on Sep 28, 2021

    @BevanLandoByte

    I am also experiencing the problem
    "sequelize": "^6.6.5"
    "tedious": "^11.0.7"

  11. devtaha commented on Nov 16, 2021

    @devtaha

    same problem I was facing
    in my case I was running sync with force true while no tables were there in db

    that why such error occurred for me
    It might help someone.

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