Rovidu Udesh
Rovidu Udesh

Reputation: 11

Cannot add or update a child row: a foreign key constraint fails?

Item - ItemID(PRI), ItemName, Price,Qty
Invoice - InvoiceID(PRI), Description(PRI), Item_ItemID(FK), Qty, Price

Relationship : one-to-many non-identifying relationship (Item.ItemID & Invoice.Item_ItemID)

ItemID is the PRIMARY KEY of Item table.
InvoiceID & Description is the PRIMARY KEY of Invoice table (COMPOSITE PRIMARY KEY).
I can INSERT ItemID to Invoice table that include in the ItemTable but when I'm trying to INSERT different ItemID it gives this ERROR...
" Cannot add or update a child row: a foreign key constraint fails " ?????

Upvotes: 1

Views: 144

Answers (1)

Taryn East
Taryn East

Reputation: 27747

This is expected behaviour.

A foreign key constraint stops you from entering an item-id into the invoice-table that does not already exist in the item-table. That's what a foreign key constraint IS.

Here's a link to the mysql foreign key docs for more info

Upvotes: 1

Related Questions