SQL Server Error 1767: Foreign Key References Invalid Table
In this article
SQL Server error 1767 means a foreign key constraint points to a table that SQL Server cannot find or resolve at the moment the statement runs. The fix is to identify the orphaned foreign key, confirm the exact name and schema of the referenced table, then either create the missing table, correct the constraint to point at the right table, or drop and recreate the foreign key. You need a login with ALTER permission on the affected table and access to the database where the constraint lives.
What You'll Need
- SQL Server Management Studio (SSMS) or Azure Data Studio connected to the instance, or another client that can run T-SQL.
- A login with permission to view system catalog views and to run ALTER TABLE on the affected table (db_owner or equivalent).
- The name of the database and, ideally, the name of the table or constraint that triggered the error.
- The original CREATE TABLE script or schema definition for the referenced table, if you have it.
Step-by-Step
- Read the full error message. Error 1767 usually appears with a second line such as "Foreign key 'FK_Orders_Customers' references invalid table 'dbo.Customers'." Note the constraint name and the referenced table name exactly as SQL Server prints them, including the schema prefix.
- Confirm you are in the right database. Run
SELECT DB_NAME();in the same query window. Foreign keys are database-scoped, so a constraint that exists in one database will not resolve if your script runs against another. If you are in the wrong database, change it withUSE YourDatabaseName;and try again. - Check whether the referenced table exists. Query the catalog:
SELECT SCHEMA_NAME(schema_id) AS SchemaName, name FROM sys.tables WHERE name = 'Customers';If no row comes back, the table is missing, was renamed, or lives in a different schema. Repeat for the referencing table to confirm it exists. - Check the schema, not just the name. A table named
Customersin schemasalesis a different object fromdbo.Customers. If the constraint saysdbo.Customersbut the table issales.Customers, the reference is invalid. List schemas withSELECT name FROM sys.schemas;and check the table's schema insys.tables. - Find the offending constraint. Run
SELECT fk.name AS ForeignKeyName, OBJECT_NAME(fk.parent_object_id) AS ReferencingTable, OBJECT_NAME(fk.referenced_object_id) AS ReferencedTable FROM sys.foreign_keys AS fk WHERE fk.name = 'FK_Orders_Customers';Ifreferenced_object_idis NULL or the name does not match, the constraint is broken or points at a missing object. - Decide the repair. If the referenced table is genuinely missing, create it with the correct columns and a primary or unique key on the referenced column. If the table exists under a different schema or name, drop the bad constraint and recreate it against the correct table. If the constraint is a leftover from a bad restore or partial deployment, drop it.
- Drop the broken constraint. Use
ALTER TABLE dbo.Orders DROP CONSTRAINT FK_Orders_Customers;Run this only after you have captured the original definition, because dropping a foreign key removes the referential integrity check until you recreate it. - Recreate the constraint correctly. Example:
ALTER TABLE dbo.Orders WITH CHECK ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers (CustomerID);Then runALTER TABLE dbo.Orders CHECK CONSTRAINT FK_Orders_Customers;to validate existing rows. If validation fails, you have orphaned rows in the child table that must be fixed or removed first. - Re-run the original statement. Execute the script, restore, or deployment step that produced error 1767. If it now succeeds, capture the corrected schema in source control so the same bad reference does not ship again.
Common Mistakes
- Assuming the table is missing when it was only renamed or moved to another schema. Always check
sys.tableswith the schema name. - Running the fix in the wrong database. Foreign keys do not cross database boundaries, so a constraint in one database cannot reference a table in another.
- Dropping the foreign key and forgetting to recreate it, leaving the child table without referential integrity.
- Recreating the constraint with
WITH NOCHECKto hide orphaned rows. That leaves bad data in place and the constraint untrusted; fix the rows instead. - Editing the constraint by hand in a script but not updating the deployment source, so the error returns on the next release.
Tips
- Script out the existing foreign key before dropping it: in SSMS, right-click the table, choose Script Table as > CREATE To, and save the output.
- Use
sp_help 'dbo.Orders'to see the table's constraints and the objects they reference in one result set. - Check for case and collation mismatches on case-sensitive instances; a table created as
Customerswill not match a reference tocustomers. - If the database was restored from a backup or copied between servers, verify that all referenced tables were included in the restore or copy.
- Keep schema changes in version-controlled migration scripts so a missing table is caught in testing rather than in production.
Troubleshooting
If the error persists after recreating the constraint, confirm you are connected to the same database and instance the application uses. Check for synonyms: a foreign key cannot reference a synonym, so if the referenced object is a synonym, point the constraint at the underlying table instead.
If ALTER TABLE ... CHECK CONSTRAINT fails, query the child table for rows whose foreign key value has no match in the parent table, for example SELECT o.CustomerID FROM dbo.Orders AS o LEFT JOIN dbo.Customers AS c ON o.CustomerID = c.CustomerID WHERE c.CustomerID IS NULL; Fix or delete those rows, then validate the constraint again.
If the table exists but SQL Server still reports it as invalid, refresh the object explorer cache or reconnect, and verify the login has at least SELECT permission on the referenced table; metadata visibility can hide objects from some logins. If the database is in a partially restored or suspect state, resolve that first, because catalog metadata may be incomplete.
Frequently Asked Questions
What causes SQL Server error 1767?
A foreign key constraint references a table that does not exist, was renamed, lives in a different schema, or is in another database. The constraint is valid syntax but the target object cannot be resolved.
Can a foreign key reference a table in another database?
No. Foreign keys are scoped to a single database. Cross-database referential integrity must be enforced with triggers, application logic, or a different design.
Can a foreign key reference a view or synonym?
No. A foreign key must reference a base table with a primary or unique key on the referenced columns. Synonyms and views are not valid targets.
Will dropping and recreating the foreign key lose data?
No, dropping a constraint does not delete rows. It only removes the integrity check until you recreate and validate the constraint.
How do I find all broken foreign keys in a database?
Query sys.foreign_keys and join to sys.tables on both parent_object_id and referenced_object_id; rows where the referenced table is missing indicate broken constraints.