Recently I bumped into an error during Oracle APEX application import and I started digging in the depths of the APEX trying to figure out what the problem was. The error message stated that one foreign key constraint failed and I hoped to find a procedure with DML statement, where this error happened.
The error back trace was pretty long... and it ended at the COMMIT statement 😐At the COMMIT statement? And not at the very DML operation? 😮 I was puzzled... staring blankly at the screen... checking three times if I traced the error correctly... but I did. 😵
And suddenly I remembered that there is a parameter related to table constraints, the parameter I never used in my whole life, which determines if the constraint is validated at the moment of DML statement execution OR later on during the transaction ending COMMIT statement. This parameter is called "deferrable". And indeed, it was a case here. In the problematic APEX table all constraints were initially deferrable.
So, what is a problem with initially deferrable constraints? I mean, they work flawlessly and will prevent invalid data to be entered in the table...
BUT when You execute the PL/SQL code and some error occurs, deferrable constraints can cause the PL/SQL error back trace to be a really misleading.
The error back trace is showing that the COMMIT statement fails and You actually have no clue, which exact DML statement caused an error.
Let's look at the following example.
I'm creating a table with 2 deferrable constrains:
(
id NUMBER(10) NOT NULL primary key,
name VARCHAR2(100),
limit NUMBER NOT NULL,
CONSTRAINT testing_constraints_name_nnl CHECK (name is not null) initially deferred deferrable,
CONSTRAINT testing_constraints_limit_c CHECK (limit <= 3000) initially deferred deferrable
SET CONSTRAINTS ALL IMMEDIATE;
If I execute this statement first and then I execute the example from above, the error trace is pinpointing the first statement with the error, which is line 2 (limit too high):
ORA-06512: at line 3
Oracle Documentation
Good to know!
ReplyDelete