Skip to content

Unnecessary migrations generated with computed columns on MSSQL #12019

Closed

Description

@descawed

Issue description

On Microsoft SQL Server, when a column in one table has the same name as a computed column in another table, generating a migration will always generate code dropping and re-creating the column in the first table, even when that column already exists in the database and the definition hasn't changed.

Expected Behavior

Generating a migration should only generate code for columns and/or entities whose definitions differ from what's in the database.

Actual Behavior

When a column in one table has the same name as a computed column in another table, generating a migration will always generate code dropping and re-creating the first column, even when the definition already matches what's in the database.

Steps to reproduce

Given entity definitions like:

@Entity({name: 'tableA'})
export class TableA {
  @Column('int')
  x!: number;
  @Column({
    type: 'bit',
    asExpression: 'CASE WHEN x <> 0 THEN 1 ELSE 0 END',
    generatedType: 'STORED'
  })
  isActive!: boolean;
}

@Entity({name: 'tableB'})
export class TableB {
  @Column('bit')
  isActive!: boolean;
}

Generate a migration and run it to deploy the tables to the database. Then, without changing anything, generate another migration. TypeORM will create a migration dropping and re-creating the isActive column in tableB.

My Environment

Dependency Version
Operating System Ubuntu 25.10
Node.js version 20.19.4
Typescript version 5.9.3
TypeORM version 0.3.28

Additional Context

I believe this bug is due to a bad join condition when TypeORM is inspecting the current state of the database schema. When I run migration:generate, one of the SQL statements that TypeORM outputs looks like the following (I've added newlines and removed the where clause):

SELECT "COLUMNS".*, "cc"."is_persisted", "cc"."definition"
FROM "MyDatabase"."INFORMATION_SCHEMA"."COLUMNS"
LEFT JOIN "sys"."computed_columns" "cc" ON COL_NAME("cc"."object_id", "cc"."column_id") = "column_name"

This joins to the computed_columns table on only the column name, not taking into account the table or schema name. This means, in the example above, the isActive columns in both tableA and tableB will match to the computed_columns row. I suspect this causes TypeORM to think that isActive in tableB has changed from a computed column to a non-computed column, so it generates a migration dropping and re-creating the column. A corrected join condition could look like this:

SELECT "COLUMNS".*, "cc"."is_persisted", "cc"."definition"
FROM "MyDatabase"."INFORMATION_SCHEMA"."COLUMNS"
LEFT JOIN "sys"."computed_columns" "cc" ON COL_NAME("cc"."object_id", "cc"."column_id") = "column_name" 
  AND OBJECT_NAME("cc"."object_id") = "COLUMNS"."TABLE_NAME"
  AND OBJECT_SCHEMA_NAME("cc"."object_id") = "COLUMNS"."TABLE_SCHEMA"

This ensures that we're joining to the column in the correct table, so the computed column definition matches only to the one actual computed column.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. PreAgile commented on Mar 31, 2026

    @PreAgile
    Contributor

    I can investigate this.

    My first step would be to add a focused SQL Server regression test for the schema diff case where a normal column shares its name with a computed column in another table, and then narrow the computed-columns join to the correct table/schema if that reproduces the issue.

  2. manal617 commented on Mar 31, 2026

    @manal617
  3. added a commit that references this issue on Mar 31, 2026
    84a0678
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions