I am trying to use SqlCommandProvider (part of FSharp.Data.SqlClient) to create a column if it does not exist and then set a constraint (self referencing). The problems is that it will not compile because it detects that the column as part of the constraint does not yet exist. I tried wrapping it in another IF block to no avail.
[<Literal>]
let CreateParentColumn = "
IF COL_LENGTH('Company', 'ParentId') IS NULL
BEGIN
ALTER TABLE Company
ADD ParentId INT
ALTER TABLE Company
ADD CONSTRAINT FK_ParentIdCompanyId FOREIGN KEY (ParentId)
REFERENCES Company(CompanyId);
END
"
type CreateParentIdColumn = SqlCommandProvider<CreateParentColumn, connectionString>
I would greatly prefer not to use dynamic sql. I was wondering if there was a way to defer evaluation so that it would run properly (note, the query itself runs fine in SQL Management Studio)
Error 1 The type provider 'FSharp.Data.SqlCommandProvider' reported an error: Foreign key 'FK_ParentIdCustomerId' references invalid column 'ParentId' in referencing table 'Company'. Could not create constraint. See previous errors.