Welcome Guest, Not a member yet? Register   Sign In
Cannot add or update a child row: a foreign key constraint fails
#11

(This post was last modified: 06-14-2022, 06:32 PM by kenjis.)

Sorry, you need the namespace.

PHP Code:
Events::on(
    'DBQuery',
    static function (\CodeIgniter\Database\Query $query) { log_message('alert', (string) $query); }
); 

You can see the query in the error page.
[Image: 2022-06-15-10-27-36.png]
Reply
#12

I'm not seeing any insert queries:
Code:
ALERT - 2022-06-14 20:32:32 --> SELECT 1
FROM `live_outputs`
WHERE `alias` = 'pEWtvZPx'
LIMIT 1
ERROR - 2022-06-14 20:32:32 --> Cannot add or update a child row: a foreign key constraint fails (`scs`.`live_outputs`, CONSTRAINT `live_outputs_ibfk_1` FOREIGN KEY (`livestream_id`) REFERENCES `live_streams` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)
CRITICAL - 2022-06-14 20:32:32 --> Cannot add or update a child row: a foreign key constraint fails (`scs`.`live_outputs`, CONSTRAINT `live_outputs_ibfk_1` FOREIGN KEY (`livestream_id`) REFERENCES `live_streams` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)
#0 /var/www/scs/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php(292): mysqli->query()
#1 /var/www/scs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(670): CodeIgniter\Database\MySQLi\Connection->execute()
#2 /var/www/scs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(598): CodeIgniter\Database\BaseConnection->simpleQuery()
#3 /var/www/scs/vendor/codeigniter4/framework/system/Database/BaseBuilder.php(1771): CodeIgniter\Database\BaseConnection->query()
#4 /var/www/scs/vendor/codeigniter4/framework/system/Model.php(239): CodeIgniter\Database\BaseBuilder->insert()
#5 /var/www/scs/vendor/codeigniter4/framework/system/BaseModel.php(738): CodeIgniter\Model->doInsert()
#6 /var/www/scs/vendor/codeigniter4/framework/system/Model.php(625): CodeIgniter\BaseModel->insert()
#7 /var/www/scs/app/Controllers/PanelLiveOutputs.php(151): CodeIgniter\Model->insert()
#8 /var/www/scs/vendor/codeigniter4/framework/system/CodeIgniter.php(832): App\Controllers\PanelLiveOutputs->add()
#9 /var/www/scs/vendor/codeigniter4/framework/system/CodeIgniter.php(419): CodeIgniter\CodeIgniter->runController()
#10 /var/www/scs/vendor/codeigniter4/framework/system/CodeIgniter.php(326): CodeIgniter\CodeIgniter->handleRequest()
#11 /var/www/scs/public/index.php(37): CodeIgniter\CodeIgniter->run()
#12 {main}
Reply
#13

Ah, when simpleQuery() gets an error, the DBQuery event is not fired!

Can't you see the SQL in the error page?
Click ( arguments )
Reply
#14

I don't see an arguments tab, only:     Backtrace Server Request Response Files Memory
Reply
#15

Not a tab. It is in Backtrace.
See my screenshot above.
Reply
#16

(This post was last modified: 06-14-2022, 07:11 PM by Sincere.)

I'm afraid I'm not seeing that:
[Image: Screen-Shot-2022-06-14-at-22-09-23.png]
Also no toolbar below, which would also be helpful.

BTW, thank you thus far for your dedication to help!
Reply
#17

(This post was last modified: 06-14-2022, 07:16 PM by kenjis.)

Oh, I see.

Mine:
SYSTEMPATH/Database/MySQLi/Connection.php : 292  —  mysqli->query ( arguments )

Yours:
SYSTEMPATH/Database/MySQLi/Connection.php : 292  —  mysqli->query ()

I don't know why your backtrace can't get the arguments.
Reply
#18

This error comes whenever we add a foreign key constraint between tables and insert records into the child table. Let us see an example.

Creating the child table.

mysql> create table ChildDemo
  -> (
  -> id int,
  -> FKPK int
  -> );
Query OK, 0 rows affected (0.86 sec)
Creating the second table.

mysql> create table ParentDemo
  -> (
  -> FKPK int,
  -> Name varchar(100)
  -> ,
  -> primary key(FKPK)
  -> );
Query OK, 0 rows affected (0.57 sec)
To add foreign key constraint.

mysql> alter table ChildDemo add constraint ConstChild foreign key(FKPK) references ParentDemo(FKPK);
Query OK, 0 rows affected (1.97 sec)
Records: 0  Duplicates: 0  Warnings: 0
After creating foreign key constraint, whenever we insert records into the first table or child table, we will get the above error.

mysql> insert into ChildDemo values(1,3);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`business`.`childdemo`, CONSTRAINT `ConstChild` FOREIGN KEY (`FKPK`) REFERENCES `parentdemo` (`fkpk`))
The error comes when you are trying to add a row for which no matching row in in the other table.

As stated

“Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table. It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.”
Do visit The Next Decor one stop for complete home decor solution
Reply
#19

Then why would inserting data through the query builder work, and the model would reject the same input?
Reply
#20

(06-14-2022, 06:45 PM)kenjis Wrote: Ah, when simpleQuery() gets an error, the DBQuery event is not fired!

This bug has been fixed in the latest develop branch.
If you can install the develop branch, check it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB