首页 > 解决方案 > SQL Server Foreign Key Constraint Error, But Data Exists

问题描述

I continue getting an insert error referencing a foreign key constraint error that I can't figure out. The related data is there... so what is causing this error?

Here's the data and insert statement:

enter image description here

Here are the relationships:

enter image description here

And here are the details on the foreign key in question:

enter image description here

The scripting on the table that created the constraint looks good to me: enter image description here

Also, through some other research, I've confirmed there are not any transactions left open and also that there are no violating FK values.

*open transaction check: SELECT er.session_id, er.open_transaction_count FROM sys.dm_exec_requests er Where open_transaction_count > 0

*violating FK info: https://www.mssqltips.com/sqlservertip/2326/find-violating-sql-server-foreign-key-values/

It may be staring me in the face and I've been staring at it too long now, not sure. But, any new set of eyes and ideas are greatly appreciated!

标签: sqlsql-serverforeign-keys

解决方案


外键的列被混淆了。DivisionId 引用permissionRoleId 等。按正确的顺序放置列。


推荐阅读