SQL Server Error 1772: Foreign Key Not Valid
In this article
Error 1772 means SQL Server cannot validate a foreign key constraint because the referenced table or column does not exist, has a different definition, or the relationship is not properly set up. To fix it, verify that the referenced table and column exist with matching data types, then drop and recreate the foreign key with the correct definition. This guide walks you through the exact steps to diagnose and resolve the error, whether you are creating a new foreign key or dealing with an existing one that has become invalid.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another client that can run T-SQL queries.
- Permissions to alter the tables involved (typically db_owner or ALTER permission on the tables).
- The names of the child (referencing) and parent (referenced) tables and columns.
Step-by-Step
- Identify the exact error message. Run the statement that caused the error and note the full message. Error 1772 typically says something like: "Foreign key 'FK_name' is not valid because the referenced table 'ParentTable' does not exist or the referenced column 'ParentColumn' does not exist." This tells you which foreign key and which referenced object is missing or mismatched.
- Check that the referenced table exists. In SSMS, expand the database, then Tables. Or run:
SELECT * FROM sys.tables WHERE name = 'ParentTable';Replace ParentTable with the actual name. If no row is returned, the table is missing or you are connected to the wrong database. Verify you are in the correct database withSELECT DB_NAME();. - Check that the referenced column exists and has a compatible data type. Run:
SELECT name, system_type_id, max_length, precision, scale FROM sys.columns WHERE object_id = OBJECT_ID('ParentTable');Compare the data type of the referenced column with the foreign key column in the child table. They must be identical in type, length, precision, and scale (except for length when using varchar/nvarchar with different lengths, which is allowed only if the referenced column is a primary or unique key with matching type). If they differ, you must alter one of the columns to match. - Verify that the referenced column is a primary key or has a unique constraint. A foreign key can only reference a column (or set of columns) that is a primary key or has a unique index. Run:
SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('ParentTable') AND is_primary_key = 1 OR is_unique = 1;If no unique index exists on the referenced column, create one:ALTER TABLE ParentTable ADD CONSTRAINT UQ_ParentColumn UNIQUE (ParentColumn);. - Check for schema differences. If the parent table is in a different schema, the foreign key definition must include the schema name (e.g.,
dbo.ParentTable). RunSELECT SCHEMA_NAME(schema_id) FROM sys.tables WHERE name = 'ParentTable';to confirm the schema. Then ensure your foreign key references the correct schema-qualified name. - Drop and recreate the foreign key if it already exists but is invalid. If the foreign key was created earlier and now fails validation (for example, after a table rename or column change), drop it and recreate it. First, find the foreign key name:
SELECT name FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID('ChildTable');. Then drop it:ALTER TABLE ChildTable DROP CONSTRAINT FK_name;. Recreate it with the correct definition:ALTER TABLE ChildTable ADD CONSTRAINT FK_name FOREIGN KEY (ChildColumn) REFERENCES ParentTable (ParentColumn);. - Test the foreign key by inserting or updating data. After recreating, try an insert into the child table with a value that exists in the parent table. Then try an insert with a non-existent value; it should fail with error 547 (conflict). If it succeeds, the foreign key is working.
Common Mistakes
- Referencing a column that is not a key. A foreign key must reference a primary key or unique column. Referencing a regular column will cause error 1772.
- Data type mismatch. For example, referencing an
intcolumn from abigintcolumn, or avarchar(50)from avarchar(100). SQL Server requires exact type compatibility (except for length in some cases). - Wrong database context. You may be connected to a different database than the one containing the tables. Always verify with
SELECT DB_NAME();. - Schema not specified. If the parent table is in a non-default schema, you must include the schema name in the REFERENCES clause.
- Case sensitivity. In case-sensitive collations, table and column names must match exactly.
Tips
- Use
sp_helpon the child table to see existing foreign keys and their definitions:EXEC sp_help 'ChildTable';. - Script out the foreign key from a working environment to compare definitions.
- When altering a column that is part of a foreign key, you must drop the foreign key first, alter the column, then recreate the foreign key.
- Consider using
WITH NOCHECKwhen adding a foreign key to an existing table with data, but be aware that existing rows are not validated. UseWITH CHECKto validate existing data.
Troubleshooting
If you still get error 1772 after following the steps, check the following:
- The referenced table is in a different database. Cross-database foreign keys are not supported. You must use triggers or application logic instead.
- The referenced table is a view. Foreign keys cannot reference views. They must reference base tables.
- The referenced column is computed. Computed columns cannot be referenced by foreign keys unless they are persisted and indexed, but even then it's not allowed. Use a regular column.
- You are using a case-sensitive collation and the names differ in case. Double-check the exact spelling.
- The foreign key name already exists. Drop the existing constraint before adding a new one with the same name.
- Permissions issue. Ensure you have ALTER permission on both tables.
If the error occurs during a restore or attach operation, the database may be missing the referenced table because of filegroup or partition issues. In that case, restore all files and ensure the table exists.
Frequently Asked Questions
What does error 1772 mean exactly?
It means SQL Server cannot validate a foreign key because the referenced table or column does not exist, or the referenced column is not a primary key or unique column, or there is a data type mismatch.
Can I create a foreign key that references a non-primary key column?
Yes, but the referenced column must have a unique constraint or unique index. Otherwise, you will get error 1772.
Why does error 1772 appear when I try to create a foreign key on a table with existing data?
If the existing data in the child table contains values not present in the parent table, the foreign key creation will fail with error 547, not 1772. Error 1772 is about the definition, not the data. Check that the referenced table and column exist and are keys.
How do I find the foreign key that is causing the error?
The error message includes the foreign key name. If not, query sys.foreign_keys to list all foreign keys and their definitions, then compare with your statement.
Can I disable the foreign key check temporarily?
You can disable a foreign key with ALTER TABLE ChildTable NOCHECK CONSTRAINT FK_name;, but that does not fix the underlying definition issue. It only bypasses validation for new data. Use it with caution.
What if the referenced table is in another schema?
You must include the schema name in the REFERENCES clause, e.g., REFERENCES schema_name.ParentTable (ParentColumn). Otherwise, SQL Server looks in the default schema of the current user.