Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 1770: Foreign Key Invalid Column Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 1770 means a foreign key constraint is pointing at a column in the referenced table that doesn't exist, or doesn't match the data type of the referencing column. The fix is to compare both tables' definitions, correct the column name or data type, and then create the constraint again. You cannot simply ignore the error: the constraint will not be created until the referenced column exists and is compatible.

What You'll Need

  • Access to SQL Server Management Studio (SSMS) or Azure Data Studio with permission to view table definitions and alter the schema.
  • The names of the child (referencing) table and parent (referenced) table involved in the error.
  • Permission to run ALTER TABLE statements, or a database administrator who can apply the fix.
  • A backup or a tested restore point before changing table structures, in case the change needs to be rolled back.

Step-by-Step

  1. Read the full error message. Error 1770 is usually followed by the constraint name and the referenced table. For example: Foreign key 'FK_Orders_Customers' references invalid column 'CustomerID' in referenced table 'dbo.Customers'. Write down the constraint name, the referenced table, and the column name it mentions. If the message does not name the column, continue to the next step.
  2. Find the foreign key definition. In SSMS Object Explorer, expand the database, then Tables, then the child table, then Keys. Right-click the constraint and choose Script Key as > CREATE To > New Query Editor Window. The generated script shows the exact columns on both sides. Menu labels can differ slightly between SSMS versions and Azure Data Studio, so look for the equivalent Keys or Constraints node if the wording is different.
  3. Inspect the referenced table's columns. Run a query against the system catalog to list the parent table's columns and data types. For example: SELECT c.name, t.name AS type_name, c.max_length, c.precision, c.scale FROM sys.columns c JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID('dbo.Customers'); Replace dbo.Customers with your referenced table. Confirm the column named in the error actually exists and note its exact spelling, including case and any spaces.
  4. Compare the referencing column. Run the same query for the child table, substituting its name. The referencing column and the referenced column must have the same data type, the same length for character types, the same precision and scale for decimal types, and the same collation for string types. A VARCHAR(20) child column cannot reference a VARCHAR(10) parent column, and an INT cannot reference a BIGINT.
  5. Fix the column name or data type. If the parent column is missing entirely, add it with ALTER TABLE dbo.Customers ADD CustomerID INT; and populate it before creating the constraint. If the name is misspelled, correct the foreign key definition to use the real column name. If the data types differ, alter one side so they match. Changing a column that already holds data can fail or truncate values, so check existing data first and take a backup.
  6. Drop the broken constraint if it exists. A constraint that failed to create usually does not exist, but if you are correcting an existing one, run ALTER TABLE dbo.Orders DROP CONSTRAINT FK_Orders_Customers; using your actual constraint name. Dropping a constraint does not delete rows, but it temporarily removes the referential integrity check, so do this during a maintenance window if the table is busy.
  7. Recreate the foreign key. Use the corrected script, for example: ALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers (CustomerID); Run it and confirm it completes without error 1770. If it succeeds, the constraint now appears under the child table's Keys node.
  8. Verify the relationship. Run a quick orphan check to confirm existing data satisfies the new constraint: SELECT COUNT(*) FROM dbo.Orders o LEFT JOIN dbo.Customers c ON o.CustomerID = c.CustomerID WHERE c.CustomerID IS NULL AND o.CustomerID IS NOT NULL; The count should be zero. If it is not, the constraint creation would have failed anyway, and you need to clean up the orphaned rows first.

Common Mistakes

  • Matching only the column name and ignoring the data type. SQL Server requires compatible types, lengths, precision, scale and collation; a name match alone is not enough.
  • Editing the child table when the problem is in the parent table. Error 1770 specifically names the referenced (parent) side, so check there first.
  • Forgetting that the referenced column must be a primary key or have a unique constraint. A foreign key cannot point at an ordinary non-unique column, even if the name and type match.
  • Running the fix on the wrong database. Confirm you are connected to the database that contains both tables, not to master or a development copy.
  • Dropping the constraint and forgetting to recreate it, which silently removes referential integrity until someone notices.

Tips

  • Use the system catalog queries in steps 3 and 4 rather than relying on the SSMS designer, because the designer can hide details such as collation and computed columns.
  • Script both tables' definitions to a query window and compare them side by side; this makes type mismatches obvious.
  • If the parent column is a computed column, it cannot be referenced by a foreign key. Use a persisted base column instead.
  • Test the corrected constraint on a development copy of the database before applying it to production, especially if you are altering column types.
  • Keep the corrected ALTER TABLE script in source control so the same mistake is not repeated in the next deployment.

Troubleshooting

If error 1770 persists after you correct the column name, re-run the catalog queries and compare every attribute: type, length, precision, scale and collation. A common cause is a collation difference between a VARCHAR child column and a VARCHAR parent column created with a different database collation. You can check collation with SELECT name, collation_name FROM sys.columns WHERE object_id = OBJECT_ID('dbo.Customers');.

If the error changes to a different number, such as a constraint conflict or a truncation warning, stop and read the new message; it usually points at existing data that violates the relationship. Clean the data before retrying the constraint. If you lack permission to alter the table, ask a database administrator to apply the script rather than working around the constraint. And if the constraint cannot be created because the parent table has duplicate values in the target column, add a unique constraint or primary key first, then retry.

Frequently Asked Questions

What does SQL Server error 1770 actually mean?

It means the foreign key definition names a column in the referenced table that does not exist, or that is not compatible with the referencing column. The constraint is rejected until both sides match.

Can I create a foreign key to a column that is not a primary key?

Yes, but the referenced column must have a unique constraint or a unique index. A foreign key cannot reference a column with duplicate values.

Will fixing the column type delete my data?

Altering a column type can fail or truncate values if existing data does not fit the new type. Back up the table and check the data before altering, and test on a copy first.

Why does the column exist but the error still appears?

Check spelling, schema (for example dbo versus another schema), data type, length and collation. Any mismatch produces the same error even when the column is present.

Does dropping the foreign key delete rows?

No. Dropping a constraint removes the integrity check only; the rows in both tables remain. You must recreate the constraint afterwards to restore enforcement.