How to fix Error 1765 on Windows Server 2025 / Windows 11

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 CONSTRAINT statement.
  • Locate any ON DELETE SET NULL or ON DELETE SET DEFAULT clause 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 ACTION instead.

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 TABLE statement in SQL Server Management Studio (SSMS) or via sqlcmd.
  • 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_keys again and confirming the delete_referential_action_desc column shows either NO_ACTION or CASCADE for the affected constraint.
  • If the application logic genuinely requires SET NULL behavior, implement it via an INSTEAD OF DELETE trigger on the parent table instead of relying on a native cascading foreign key action.