SQL Server Error 1771: Fix Foreign Key Reference
In this article
SQL Server error 1771 occurs when you try to create a foreign key that references a column that is not a primary key or unique constraint, or when the referenced table or column does not exist. The exact message is: "Cannot create foreign key '%.*ls' because it references object '%.*ls'." To fix it, ensure the referenced column has a primary key or unique constraint, and that both tables exist and are spelled correctly.
What You'll Need
- SQL Server Management Studio (SSMS) or another client such as Azure Data Studio, connected to your database.
- Permission to alter the tables involved (ALTER permission on both tables).
- The names of the referencing (child) and referenced (parent) tables and the columns that should be linked.
Step-by-Step
- Identify the exact error details. Run the failing CREATE TABLE or ALTER TABLE statement in SSMS. The error message will name the foreign key and the referenced object. Note both names; the referenced object is the parent table or column that SQL Server cannot find or validate.
- Verify the referenced table exists. In Object Explorer, expand your database and the Tables folder. If the parent table is missing, create it first or correct the spelling in your foreign key definition. You can also run:
SELECT * FROM sys.tables WHERE name = 'YourParentTable';and replace the name as needed. - Check 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 either the primary key or has a UNIQUE constraint. In Object Explorer, right-click the parent table, choose Design, and look for the key icon next to the column. Alternatively, run:
SELECT * FROM sys.key_constraints WHERE parent_object_id = OBJECT_ID('YourParentTable');to list primary key and unique constraints. - If the referenced column lacks a unique constraint, add one. For a single column:
ALTER TABLE ParentTable ADD CONSTRAINT UQ_ParentColumn UNIQUE (ParentColumn);For multiple columns, create a composite unique constraint on the exact column list you intend to reference. Note that adding a unique constraint fails if duplicate values exist; remove duplicates first. - Confirm data types match. The referencing column and the referenced column must have the same data type, length, precision, scale, and collation (for character columns). For example, an INT child column cannot reference a BIGINT parent column. Check both columns in the table design or with:
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTable'; - Verify the referenced object is in the same database. Foreign keys cannot reference objects in another database. If your parent table is in a different database, you must either move it to the same database or enforce the relationship through triggers or application logic.
- Check schema and ownership. If the parent table is in a different schema, include the schema name in the REFERENCES clause, for example
REFERENCES dbo.ParentTable (ParentColumn). Also ensure the table is not owned by a different user in a way that affects resolution (rare in modern SQL Server, but possible in legacy setups). - Re-create the foreign key with the correct syntax. Use explicit schema and column names. For example:
ALTER TABLE ChildTable ADD CONSTRAINT FK_Child_Parent FOREIGN KEY (ParentID) REFERENCES dbo.ParentTable (ParentID);If the error persists, double-check each item above. - Test the constraint. After creation, try inserting a row into the child table with a parent value that does not exist; it should fail. Then insert a valid row; it should succeed. This confirms the foreign key is active.
Common Mistakes
- Referencing a non-key column. The most frequent cause: the parent column is neither a primary key nor unique. Adding a unique index is not enough; you need a UNIQUE constraint or primary key.
- Mismatched data types. Even if both columns look like integers, one might be INT and the other SMALLINT or BIGINT. Always verify.
- Wrong table or column name. Typos or using the wrong schema (e.g., omitting dbo.) cause SQL Server to look for a non-existent object.
- Cross-database reference. Foreign keys cannot span databases; attempting to do so raises error 1771.
- Duplicate values in the parent column. If you try to add a unique constraint to a column with duplicates, the constraint creation fails, and the foreign key will still fail.
- Case sensitivity. In a case-sensitive collation, table and column names must match exactly.
Tips
- Use SSMS's table designer to visually confirm primary keys and unique constraints before writing the foreign key script.
- Script out the parent table's creation to see its exact key definitions.
- When creating a foreign key on multiple columns, the order of columns in the FOREIGN KEY clause must match the order in the referenced unique constraint.
- If you need to enforce referential integrity across databases, consider using triggers or application-level checks, but be aware of performance and consistency trade-offs.
- Always test foreign key changes in a development environment before applying to production.
Troubleshooting
If you still get error 1771 after following the steps, try these checks:
- Confirm the referenced object is not a view or table-valued function. Foreign keys can only reference tables, not views.
- Check for hidden characters or spaces in names. Use
SELECT OBJECT_NAME(object_id) FROM sys.objects WHERE name LIKE '%YourTable%';to see exact names. - Ensure the database compatibility level supports the feature. Foreign keys are supported in all versions, but some syntax variations may behave differently.
- Look for disabled or untrusted constraints. If the parent table has a disabled primary key or unique constraint, the foreign key cannot be created. Re-enable it with
ALTER INDEX ... REBUILDorALTER TABLE ... ADD CONSTRAINT. - Review the full error message. Sometimes error 1771 is accompanied by additional messages that pinpoint the exact object.
- Use SQL Server Profiler or Extended Events to capture the exact statement. This helps confirm you are altering the intended table.
If none of these resolve the issue, consider posting the exact CREATE TABLE scripts for both tables on a forum like Stack Overflow, including the full error message.
Frequently Asked Questions
Can a foreign key reference a column that is not a primary key?
Yes, but only if that column has a UNIQUE constraint. A foreign key can reference any column or set of columns that is either the primary key or has a unique constraint.
Why does error 1771 mention an object that I know exists?
SQL Server may be looking in the wrong schema, or the object name might be misspelled. Also, if the object is a view or a table in another database, it cannot be referenced. Verify the schema and object type.
Can I create a foreign key across databases?
No. Foreign keys are scoped to a single database. To enforce referential integrity across databases, you must use triggers or application logic.
What if the referenced column has duplicate values?
You cannot create a unique constraint on a column with duplicates, so the foreign key will fail. Remove duplicates first, then add the unique constraint and foreign key.
Does the order of columns matter in a composite foreign key?
Yes. The columns in the FOREIGN KEY clause must match the order of columns in the referenced unique constraint or primary key.
How can I find all foreign keys that reference a table?
Use: SELECT * FROM sys.foreign_keys WHERE referenced_object_id = OBJECT_ID('YourTable');