Skip to content

SQL Server schema upgrade fails when creating a filtered index based on a new column #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MichaelJLiu opened this issue Apr 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@MichaelJLiu
Copy link

To perform a schema upgrade that simultaneously adds a new [Field] and a new [Index] whose Filter references the new field, DataObjects.Net generates a single SQL batch that contains the following:

  • An ALTER TABLE statement for the new column
  • A CREATE INDEX statement for the new index

For some reason, however, SQL Server does not allow the WHERE clause of a CREATE INDEX statement to reference a column that is added in the same batch.

-- Repro for SQL Server 2019:
CREATE TABLE T (A int);
GO
ALTER TABLE T ADD X int, Y int, Z int;
CREATE INDEX IX_T ON T (X) INCLUDE (Y) WHERE (Z IS NOT NULL); -- Invalid column name 'Z'.

Therefore, the schema upgrade fails:

Xtensive.Orm.SyntaxErrorException: SQL error occurred.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Z'.

Suggested resolution: Execute the ALTER TABLE and CREATE INDEX statements in separate batches.

@alex-kulakov alex-kulakov added the bug Something isn't working label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants