[MDEV-25712] Foreign key error 1761 not caught by stored routine exception handler Created: 2021-05-18  Updated: 2023-04-27

Status: Confirmed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB, Stored routines
Affects Version/s: 5.5, 10.0, 10.1, 10.1.48, 10.2.38, 10.3.29, 10.4.19, 10.5.10, 10.6.0, 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.4, 10.5

Type: Bug Priority: Major
Reporter: Hartmut Holzgraefe Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: upstream


 Description   

Error code 1761 "Foreign key constraint ... would lead to a duplicate entry" can't be caught within a stored routines exception handler.

How to reproduce:

DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;
 
CREATE TABLE t1 ( id serial, f int, msg varchar(100), index(f));
 
CREATE TABLE t2 ( id serial, f int, unique(f), constraint `f` foreign key (f) references t1 (f) ON UPDATE CASCADE);
 
insert into t1 values(1, 23, 'abc');
insert into t1 values(2, 42, 'xyz');
 
INSERT INTO t2 VALUES(1, 23);
INSERT INTO t2 VALUES(2, 42);
 
DROP PROCEDURE IF EXISTS p1;
 
DELIMITER //
 
CREATE PROCEDURE p1()
BEGIN
  DECLARE exit HANDLER FOR  SQLEXCEPTION
  BEGIN
    SELECT 42;
  END; 
  UPDATE t1 SET f=23 WHERE f=42;
END;
 
//
 
DELIMITER ;
 
CALL p1();

Expected result, due to the error handler kicking in:

+----+
| 42 |
+----+
| 42 |
+----+
1 row in set (0.00 sec)

Actual result:

ERROR 1761 (23000): Foreign key constraint for table 't1', record '2' would lead to a duplicate entry in table 't2', key 'f'



 Comments   
Comment by Marko Mäkelä [ 2021-05-20 ]

As far as I can tell, the unexpected error is being reported by the SQL layer, in response to a HA_ERR_FOREIGN_DUPLICATE_KEY return value that InnoDB mapped its original DB_FOREIGN_DUPLICATE_KEY into. Maybe handler::print_error() should invoke the exception handler?

Generated at Thu Feb 08 09:39:46 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.