You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the Bug
We need to add two columns to a table and then populate the new columns with some data.
Doing this in a single migration fails with error Invalid column name
Steps to Reproduce
Steps to reproduce the behavior:
My migrations look like
IF NOT EXISTS ( SELECT*FROMINFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME ='pass'AND COLUMN_NAME ='date_created')
BEGINALTERTABLE [pass]
ADD date_created datetime NOT NULL DEFAULT (GETDATE());
END
IF NOT EXISTS ( SELECT*FROMINFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME ='pass'AND COLUMN_NAME ='date_updated')
BEGINALTERTABLE [pass]
ADD date_updated datetime NOT NULL DEFAULT (GETDATE());
END
UPDATE [pass]
SET date_created = start_date, date_updated = start_date;
Failed to execute migration: migration failed: Invalid column name 'date_updated'. in line 14: IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pass' AND COLUMN_NAME = 'date_created')
BEGIN
ALTER TABLE [pass]
ADD date_created datetime NOT NULL DEFAULT (GETDATE());
END
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'pass' AND COLUMN_NAME = 'date_updated')
BEGIN
ALTER TABLE [pass]
ADD date_updated datetime NOT NULL DEFAULT (GETDATE());
END
UPDATE [pass]
SET date_created = start_date, date_updated = start_date;
(details: mssql: Invalid column name 'date_updated'.)
Expected Behavior
The three queries should run in a sequence, and the update query should execute successfully.
Note that, these queries run without errors in Azure Data Studio.
Also, if we add the update query in a separate migration, it runs without errors.
Migrate Version
v4.17.1-0.20240102204802-0d4158977486
Loaded Source Drivers
sqlserver
Loaded Database Drivers
sqlserver
Go Version
go version go1.22.3 linux/amd64
Stacktrace
NA -
Additional context
NA -
The text was updated successfully, but these errors were encountered:
And it seems that the ALTER TABLE changes aren't applied until the ExecContext block exits.
In which case, the new columns don't yet exist when the update is executed.
So, we tried to change the context for the update statement
Putting the update statement inside an SQL EXEC block did the trick.
EXEC('UPDATE pass SET date_created = start_date, date_updated = start_date')
Iit would be great if someone could confirm if this is the correct approach.
Describe the Bug
We need to add two columns to a table and then populate the new columns with some data.
Doing this in a single migration fails with error
Invalid column name
Steps to Reproduce
Steps to reproduce the behavior:
Expected Behavior
The three queries should run in a sequence, and the update query should execute successfully.
Note that, these queries run without errors in Azure Data Studio.
Also, if we add the update query in a separate migration, it runs without errors.
Migrate Version
v4.17.1-0.20240102204802-0d4158977486
Loaded Source Drivers
sqlserver
Loaded Database Drivers
sqlserver
Go Version
go version go1.22.3 linux/amd64
Stacktrace
Additional context
The text was updated successfully, but these errors were encountered: