Trying to insert to a table 'tb_account'
from a procedure Add_Account
. Where i am getting from 'tb_account
' table incrementing it (lets say it @newid
) and inserting data through id
. (id
is not auto incremented).
Then from the current procedure, calling other procedure 'Add_transactionHistory'
to insert to other table using id
as foreign key which i just created and inserted data to 'tb_account'
.
second procedure is giving error: Cannot add or update a child row: a foreign key constraint fails.
Below is the code for both procedures.
Add_Account
procedure
PROCEDURE Add_Account(IN p_name tinytext, IN p_ph tinytext, IN p_bal decimal, IN p_isActive tinyint(1), IN p_userId int)
BEGIN
#insert to tb_account
CALL GetNewId('tb_account', @newid);
INSERT INTO `tb_account` (`Id`, `Name`, `PhoneNo`, `Balance`, `IsActive`, `CreatedAt`, `UserId`)
VALUES (@newid, p_name , p_ph , p_bal , p_isActive , now(), p_userId );
#insert to transachistory
set @sid = @newid;
CALL Add_TransactionHistory(@sid, null, '', p_bal, now(), '', null, null, 0, p_userId, 1);
END
Add_TransactionHistory
procedure
PROCEDURE Add_TransactionHistory(IN sid int, IN rid int, IN bnk varchar(400), IN amt decimal, IN
tdate datetime, IN ds varchar(400), IN img mediumblob, IN soid,
IN bizid int, IN uid int)
BEGIN
CALL Insert_tb_transachistory(sid, rid, bnk, amt, tdate, ds, img, soid, bizid, uid);
END
in Add_TransactionHistory
sid
is given from Add_Account
procedure.
Whenever i tried to run prcedure separately they run. I have tried logging in Add_TransactionHistory
procedure everything goes right. But from executing Add_Account
, i get error. I double checked id is generated for each account successfully.
I also browse similar questions but in vein.
I am trying to get data inserted into both tables.
I got the problem.
Across all the procedures I was using same names for some common purpose. I just renamed them now its working as expected.