Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint «FK_Sup_Item_Sup_Item_Cat». The conflict occurred in database «dev_bo», table «dbo.Sup_Item_Cat». The statement has been terminated.
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id
is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat
into the column, corresponding to the sup_item.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
asked Jun 3, 2010 at 12:24
SmartestVEGASmartestVEGA
8,43526 gold badges86 silver badges142 bronze badges
1
Your table dbo.Sup_Item_Cat
has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help
‘dbo.Sup_Item_Cat
‘. See which column that FK is on, and which column of which table it references. You’re inserting some bad data.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jun 3, 2010 at 12:29
2
The answer on this page by Mike M. is correct:
The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
What is missing from that answer is: You must build the table containing the primary key first.
You must insert data into the parent table, containing the primary key, before attempting to insert data into the foreign key.
After adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jan 17, 2014 at 20:50
plasmasnakeneoplasmasnakeneo
2,2211 gold badge13 silver badges16 bronze badges
1
You are trying to insert a record with a value in the foreign key column that doesn’t exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
Katianie
5891 gold badge9 silver badges38 bronze badges
answered Jun 3, 2010 at 12:31
Matthew SmithMatthew Smith
1,2871 gold badge9 silver badges19 bronze badges
0
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jun 3, 2010 at 12:27
Justin NiessnerJustin Niessner
242k40 gold badges408 silver badges536 bronze badges
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides ‘123123’ looks suspect as well.
answered Jun 3, 2010 at 12:44
CobusveCobusve
1,57210 silver badges23 bronze badges
All the fields have to match EXACTLY.
For example, sending ‘cat dog’ is not the same as sending ‘catdog’.
To troubleshoot this: Script out the FK code from the table you ae inserting data into, take note of the «Foreign Key» that has the constraints and make sure those fields’ values match EXACTLY as they were in the table throwing the FK Constraint error.
Fix the fields.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered May 30, 2014 at 21:06
John WaclawskiJohn Waclawski
9361 gold badge11 silver badges20 bronze badges
0
My insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Oct 23, 2015 at 13:40
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
answered Jan 6, 2014 at 19:27
- run sp_helpconstraint
- pay attention to the constraint_keys column returned for the foreign key
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Apr 12, 2014 at 16:20
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1
and column2
, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
answered Sep 12, 2020 at 6:45
The problem was reproducible and intermittent for me using mybatis.
I had correct DB configuration (PK, FK, auto increment etc)
I had correct order of insertions (parent records first); in debug I could see the parent record inserted with respective PK and just after that next statement failed with inserting the child record with the correct FK inside.
The problem was fixed by reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
The code that failed before started to work.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Apr 28, 2021 at 22:12
MikeMike
20.1k25 gold badges98 silver badges141 bronze badges
I used code-first migrations to build my database for an MVC 5 application. The seed method in my configuration.cs file was causing the issue, creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered May 14, 2014 at 19:12
Paulie22Paulie22
1111 gold badge1 silver badge9 bronze badges
The error you’re encountering is due to a violation of the FOREIGN KEY constraint FK_Sup_Item_Sup_Item_Cat
when attempting to insert a value into the client_id column of the sup_item table. The error message indicates that the value you’re trying to insert does not exist in the referenced table Sup_Item_Cat in the dev_bo database.
To resolve this issue, you should ensure that the value you’re inserting into the client_id column matches an existing value in the referenced table Sup_Item_Cat. You can verify the available values in the Sup_Item_Cat
table and make sure you’re providing a valid client_id.
Here’s an example query to retrieve the existing client_id values from the Sup_Item_Cat table:
SELECT client_id
FROM dbo.Sup_Item_Cat;
Review the output of this query and confirm that the value you’re trying to insert into the client_id column exists in the Sup_Item_Cat
table.
To identify which other tables and columns a particular table depends on, you can use the information from the Keys folder and Depends on dbForge Studio for SQL Server.
answered Jun 22 at 15:18
1
I experienced same issue while running the query:
INSERT INTO [dbo].[work_flow_actions] ([work_flow_id] ,[actions_id]) VALUES (70004, 150044)
Meanwhile, I confirmed that the values 70004 and 150044 exist in the corresponding tables.
I fixed this by including the database name as follows:
INSERT INTO [TIS].[dbo].[work_flow_actions] ([work_flow_id] ,[actions_id]) VALUES (70004, 150044)
answered Jun 28 at 1:11
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint «FK_Sup_Item_Sup_Item_Cat». The conflict occurred in database «dev_bo», table «dbo.Sup_Item_Cat». The statement has been terminated.
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id
is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat
into the column, corresponding to the sup_item.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
asked Jun 3, 2010 at 12:24
SmartestVEGASmartestVEGA
8,43526 gold badges86 silver badges142 bronze badges
1
Your table dbo.Sup_Item_Cat
has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help
‘dbo.Sup_Item_Cat
‘. See which column that FK is on, and which column of which table it references. You’re inserting some bad data.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jun 3, 2010 at 12:29
2
The answer on this page by Mike M. is correct:
The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
What is missing from that answer is: You must build the table containing the primary key first.
You must insert data into the parent table, containing the primary key, before attempting to insert data into the foreign key.
After adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jan 17, 2014 at 20:50
plasmasnakeneoplasmasnakeneo
2,2211 gold badge13 silver badges16 bronze badges
1
You are trying to insert a record with a value in the foreign key column that doesn’t exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
Katianie
5891 gold badge9 silver badges38 bronze badges
answered Jun 3, 2010 at 12:31
Matthew SmithMatthew Smith
1,2871 gold badge9 silver badges19 bronze badges
0
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Jun 3, 2010 at 12:27
Justin NiessnerJustin Niessner
242k40 gold badges408 silver badges536 bronze badges
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides ‘123123’ looks suspect as well.
answered Jun 3, 2010 at 12:44
CobusveCobusve
1,57210 silver badges23 bronze badges
All the fields have to match EXACTLY.
For example, sending ‘cat dog’ is not the same as sending ‘catdog’.
To troubleshoot this: Script out the FK code from the table you ae inserting data into, take note of the «Foreign Key» that has the constraints and make sure those fields’ values match EXACTLY as they were in the table throwing the FK Constraint error.
Fix the fields.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered May 30, 2014 at 21:06
John WaclawskiJohn Waclawski
9361 gold badge11 silver badges20 bronze badges
0
My insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Oct 23, 2015 at 13:40
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
answered Jan 6, 2014 at 19:27
- run sp_helpconstraint
- pay attention to the constraint_keys column returned for the foreign key
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Apr 12, 2014 at 16:20
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1
and column2
, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
answered Sep 12, 2020 at 6:45
The problem was reproducible and intermittent for me using mybatis.
I had correct DB configuration (PK, FK, auto increment etc)
I had correct order of insertions (parent records first); in debug I could see the parent record inserted with respective PK and just after that next statement failed with inserting the child record with the correct FK inside.
The problem was fixed by reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
The code that failed before started to work.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered Apr 28, 2021 at 22:12
MikeMike
20.1k25 gold badges98 silver badges141 bronze badges
I used code-first migrations to build my database for an MVC 5 application. The seed method in my configuration.cs file was causing the issue, creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
philipxy
14.9k6 gold badges39 silver badges85 bronze badges
answered May 14, 2014 at 19:12
Paulie22Paulie22
1111 gold badge1 silver badge9 bronze badges
The error you’re encountering is due to a violation of the FOREIGN KEY constraint FK_Sup_Item_Sup_Item_Cat
when attempting to insert a value into the client_id column of the sup_item table. The error message indicates that the value you’re trying to insert does not exist in the referenced table Sup_Item_Cat in the dev_bo database.
To resolve this issue, you should ensure that the value you’re inserting into the client_id column matches an existing value in the referenced table Sup_Item_Cat. You can verify the available values in the Sup_Item_Cat
table and make sure you’re providing a valid client_id.
Here’s an example query to retrieve the existing client_id values from the Sup_Item_Cat table:
SELECT client_id
FROM dbo.Sup_Item_Cat;
Review the output of this query and confirm that the value you’re trying to insert into the client_id column exists in the Sup_Item_Cat
table.
To identify which other tables and columns a particular table depends on, you can use the information from the Keys folder and Depends on dbForge Studio for SQL Server.
answered Jun 22 at 15:18
1
I experienced same issue while running the query:
INSERT INTO [dbo].[work_flow_actions] ([work_flow_id] ,[actions_id]) VALUES (70004, 150044)
Meanwhile, I confirmed that the values 70004 and 150044 exist in the corresponding tables.
I fixed this by including the database name as follows:
INSERT INTO [TIS].[dbo].[work_flow_actions] ([work_flow_id] ,[actions_id]) VALUES (70004, 150044)
answered Jun 28 at 1:11
Error: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint «FK__Item__order__3AE27131». The conflict occurred in database «pmall», table «dbo.ItemSaved», column ‘id’.
Here’s my table:
ItemSavedUnits
- id
- ItemID (is set in this table a FK to Item.id)
- …etc.
Here’s my insert statement:
insert into ItemSavedUnits (ItemID, name, Price)
select ItemID, name,Price
from ItemUnits where ItemID = 92439
I don’t really understand why if I a FK constraint on ItemSavedUnits.ItemID that is related to Item.ItemID and ItemUnits has no constraints at all why I’m getting a problem inserting into ItemSavedUnits. The ItemID I’m tryign to insert does exist in the Item table.
asked Dec 1, 2009 at 19:38
PositiveGuyPositiveGuy
46.7k111 gold badges305 silver badges472 bronze badges
1
Are you absolutely sure that ItemId 92439 exists in the Item table, and not just in ItemUnits?
or
Is your select statement returning null?
answered Dec 1, 2009 at 19:40
7
It looks like you need a row in ItemUnits with that ID first — what does the SELECT statement part of your insert return? No rows?
Also, is there a trigger on the ItemSavedUnits table that could be causing problems?
answered Dec 1, 2009 at 19:43
SqlRyanSqlRyan
33.1k33 gold badges115 silver badges200 bronze badges
Your foreign key constraint violation doesn’t appear to deal with the ItemSavedUnits
table — the violation exception is being thrown by the constraint on the ItemSaved
table, not the ItemSavedUnits
table. Is there a trigger on ItemSavedUnits
that’s trying to insert into ItemSaved
?
answered Dec 1, 2009 at 19:45
DathanDathan
7,2963 gold badges27 silver badges46 bronze badges
- Remove From My Forums
-
Question
-
I have recently installed a reporting software for vmware vsphere however I am getting a SQL error. I have opened a support request with vmware but so far they have not come up with a solution. The error is Caused by: com.microsoft.sqlserver.jdbc.SQLServerException:
The INSERT statement conflicted with the FOREIGN KEY constraint «FK_CB_VSM_NETWORK_VC_ID». The conflict occurred in database «VCChargebackVCC02», table «dbo.CB_VSM_SERVER», column ‘VC_ID’. I dont know much about SQL so I am lost as far as troubleshooting
is concerned. If anyone has any ideas I’d love to hear them.
— ATG —
Answers
-
The issue is most likely in your reporting software or in the associated database setup. You should contact the vendor of that software for support.
-
Marked as answer by
Saturday, September 15, 2012 7:20 AM
-
Marked as answer by
Здравствуйте!
Есть две таблицы которые связаны ключом, при создании строки с с этим ключом SQL жалуется:
Сообщение 547, уровень 16, состояние 0, строка 2
Конфликт инструкции INSERT с ограничением FOREIGN KEY «FK$ChequePos@ChequeId#ChequeHead@Id». Конфликт произошел в базе данных «RER», таблица «dbo.ChequeHead», column ‘Id’.
В таблице ChequeHead есть Id: 20336035
Пытаюсь создать так:
SQL | ||
|
В чем причина? Как создать строку?