Troubleshooting Guide: SQL Server Error 1765 – Foreign Key Creation Failed on Computed Column
Why This Error Happens
This error occurs because SQL Server restricts the ON DELETE referential action for foreign keys that reference a computed column in the referencing table. When a foreign key references a computed column, SQL Server only permits NO ACTION or CASCADE as valid delete actions — options like SET NULL or SET DEFAULT are not supported because they cannot be logically applied to a value derived from an expression. This typically happens when a developer scripts a foreign key constraint with an unsupported ON DELETE clause against a table schema that includes computed columns, often after a schema migration or an automated DDL generation tool creates the constraint incorrectly.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Foreign Key and Computed Column
- Run the following query to locate foreign keys referencing computed columns in your database:
SELECT
fk.name AS FK_Name,
OBJECT_NAME(fk.parent_object_id) AS Table_Name,
c.name AS Column_Name,
c.is_computed
FROM sys.foreign_keys fk
INNER JOIN sys.foreign_key_columns fkc
ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c
ON fkc.parent_object_id = c.object_id
AND fkc.parent_column_id = c.column_id
WHERE c.is_computed = 1;
- Note the exact table name, column name, and constraint name referenced in the Error 1765 message.
Step 2: Review and Correct the ON DELETE Clause
- Open the DDL script or migration tool output that generated the
ALTER TABLE ... ADD CONSTRAINTstatement. - Locate any
ON DELETE SET NULLorON DELETE SET DEFAULTclause tied to the computed column’s foreign key. - Replace the unsupported action with a supported one, for example:
ALTER TABLE dbo.OrderDetails
ADD CONSTRAINT FK_OrderDetails_Orders
FOREIGN KEY (OrderID) REFERENCES dbo.Orders(OrderID)
ON DELETE CASCADE;
- If cascading deletes are not desired, use
ON DELETE NO ACTIONinstead.
Step 3: Validate the Computed Column Definition
- Confirm whether the computed column truly needs to be part of the foreign key relationship by running:
EXEC sp_help 'dbo.OrderDetails';
- If the computed column logic is overly complex or unnecessary for the relationship, consider persisting the column (
PERSISTED) or restructuring the schema so the foreign key references a physical, non-computed column instead.
Step 4: Re-run the Constraint Creation Script
- Execute the corrected
ALTER TABLEstatement in SQL Server Management Studio (SSMS) or viasqlcmd. - Check the Messages tab in SSMS for confirmation that the constraint was created without Error 1765 reappearing.
Alternative or Verification Steps
- Verify the fix by querying
sys.foreign_keysagain and confirming thedelete_referential_action_desccolumn shows eitherNO_ACTIONorCASCADEfor the affected constraint. - If the application logic genuinely requires
SET NULLbehavior, implement it via anINSTEAD OF DELETEtrigger on the parent table instead of relying on a native cascading foreign key action.