diff --git a/src/driver/sqlserver/SqlServerQueryRunner.ts b/src/driver/sqlserver/SqlServerQueryRunner.ts index 0973b6308b..7c9b349bfd 100644 --- a/src/driver/sqlserver/SqlServerQueryRunner.ts +++ b/src/driver/sqlserver/SqlServerQueryRunner.ts @@ -875,7 +875,7 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner oldColumn.name = newColumn.name; } - if (this.isColumnChanged(oldColumn, newColumn)) { + if (this.isColumnChanged(oldColumn, newColumn, false)) { upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN ${this.buildCreateColumnSql(table, newColumn, true, false)}`)); downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN ${this.buildCreateColumnSql(table, oldColumn, true, false)}`)); } @@ -940,16 +940,19 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner } if (newColumn.default !== oldColumn.default) { - if (newColumn.default !== null && newColumn.default !== undefined) { - const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, newColumn.name); - upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${newColumn.default} FOR "${newColumn.name}"`)); - downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`)); - } else if (oldColumn.default !== null && oldColumn.default !== undefined) { + // (note) if there is a previous default, we need to drop its constraint first + if (oldColumn.default !== null && oldColumn.default !== undefined) { const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, oldColumn.name); upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`)); downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${oldColumn.default} FOR "${oldColumn.name}"`)); } + + if (newColumn.default !== null && newColumn.default !== undefined) { + const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, newColumn.name); + upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${newColumn.default} FOR "${newColumn.name}"`)); + downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`)); + } } await this.executeQueries(upQueries, downQueries);