SQL Server PK problem
-
08-10-2019 - |
Question
This is a screenshot of my db when I execute this query
INSERT INTO Products(CategoryID, TypeID, BarCode, ArtNumber, ProductName, Price, SelfPrice, PriceWithOutAWD, UnitsInStock, Comment)
VALUES('9', '1', '23', '23', 'kut', '20.29', '18.29', '19.10', '23', '19')
it shows such error
Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Type1". The conflict occurred in database "AcidDB", table "dbo.Type", column 'ID'. The statement has been terminated.
Solution
You're trying to insert a record into Products with a TypeID that doesn't exist in the table Type.
There is not record in the table Type with the ID "1".
OTHER TIPS
Without seeing the FK definition, this is an educated guess:
Your type table does not have an entry with ID of 1.
So, when trying to insert a 1 to the TypeId
column, a matching entry on the Type
table can't be found and the constraint fails.
Unrelated:
Why are you passing a string ('1'
) to an INTEGER
parameter?
you are inserting the typeId 1 into your table, but it seems that there is no typeId 1 defined in your type-table.
btw: you don't need to put the int-values into ''!
You don't really ask a question, but I guess you don't understand the error message...
It is very simple: your Products.TypeId field has a FK constraint linking to table Type.ID.
Since there is no type.id of value '1' your insert triggers an error.
Maybe, as pointed by karlis, you have a type 1 (without quotes) !