简体   繁体   中英

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

I have two tables,

purchase(purchId,date,empId,supId)
item(itemID,name,qty,unit_price)

And,

purchase_item(purchId,itemID,qty,unit_price)

the purchase table and the item table gets updated fine, but when I try to update the purchase_item ,

Cannot add or update a child row: a foreign key constraint fails ( bookshop . purchase_item , CONSTRAINT purchase_item_ibfk_1 FOREIGN KEY ( purchId ) REFERENCES purchase ( purchId ) ON DELETE CASCADE ON UPDATE CASCADE)

Update Statement used,

Insert into purchase_item values('" + invoice + "','" + ItemId + "','" + qty + "','" + unitPrice + "')"; (working on a C# project)

Code,w

int ItemId = Convert.ToInt32(textBox3.Text);
        string ItemName = textBox4.Text;
        string Category = textBox5.Text;
        int qty = Convert.ToInt32(textBox7.Text);
        double UnitPrice = Convert.ToDouble(textBox6.Text);
        string supname = textBox8.Text;
        int supId = 12345;
        int invoice = Convert.ToInt32(textBox9.Text);
        string query1 = "Insert into item values('" + ItemId + "','" + ItemName + "','" + Category + "','" + qty + "','" + UnitPrice + "')";
        int rows = db.SaveUpdateDelete(query1);

        DateTime d = DateTime.Now;
        int day = d.Day;
        int month = d.Month;
        int year = d.Year;

        string dt = year + "/" + month + "/" + day;


        string query = "Insert into purchase values("+invoice+",'"+dt+"',"+username+","+supId+")";
        string query2 = "Insert into purchase_item values('" + invoice + "','" + ItemId + "','" + qty + "','" + UnitPrice + "')";

        int rows1 = db.SaveUpdateDelete(query);
        int rows2 = db.SaveUpdateDelete(query2);
        if  (rows==1 && rows1==1 && rows2==1)
        {


            MessageBox.Show("Data Entered Successfully.");
        }
        else
        {
            MessageBox.Show("Failed to Insert Data.");
        }           

You're getting this error because you're trying to add a row to purchase_item that does not have a valid value for the itemID or purchId field based on the values currently stored in purchase and item tables. If you post some more code I can help you diagnose the specific cause.

try to follow steps, hopefully, you will get what is the issue:

  1. first check whether your query is successfully executed on following lines

string query1 = "Insert into item values('" + ItemId + "','" + ItemName + "','" + Category + "','" + qty + "','" + UnitPrice + "')"; int rows = db.SaveUpdateDelete(query1);

what you are getting in "rows", then

change your code for inserting the purchase first, then create items list and then insert purchase_items.

if purchase ID you are using in Purchase_items then.

Another could you please confirm, on which line of code you are facing this issue.

UPDATE: do one more thing, please pass static values first to verify if SP is working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM