How do I catch an foreign key integrity constaint?

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
mikeho
Posts: 41
Joined: Thu 11 Nov 2004 21:23
Location: Middle Tennessee USA

How do I catch an foreign key integrity constaint?

Post by mikeho » Fri 16 May 2008 22:16

Not really an ODAC error, but neither the EOraError exception nor TOraTable's OnDeleteError are helping catch the constraint problem.

Here is the setup:

1. Customer table has a field with a foreign key constraint to a Definitions table.

2. The Definitions table has a Master-Detail relationship to a Lines table, and the Lines table has a Master-Detail relationship to a Chunks table.

3. The Customer table has a BeforeDelete event which deletes the detail records from the Lines table (in reverse order) and the Lines table has a BeforeDelete event that deletes the detail records from the Chunks table.

4. I have a transaction set up to allow for rollback when an error happens.

Now Deleting a record from the definition table that exists in the customer table field throws the integrity constraint error. That is great!

However when I put a try-except block around the table delete call, none of the defined exceptions are catching the error. I only get the awful Delphi generic exception dialog and the irritating error clunk sound.

I am wanting to catch the error so users don't see this useless message:

ORA-02091: transaction rollled back
ORA-02092: integrity constraint (ALPS.CUSTOMER_FK18) violated - child record found.


but instead see somthing like:

This definition is in use and cannot be deleted.

I have tried the following, none of which show the MessageDlg.

Code: Select all

procedure TfrmExportEdit.btnDeleteClick(Sender:TObject);
begin
  if (MessageDlg('Are you sure you want to delete this definition?',
      mtInformation, [mbYes, mbNo], 0)  mrYes) then
    Exit;
  OraSession.StartTransaction;
  try
    try
      tblDefinitions.Delete;
    except
      on e: EOraError do
      begin
        OraSession.Rollback;
        MessageDlg('#1 - ' + e.Message, mtWarning, [mbOK], 0);
      end;
      on e: EDatabaseError do
      begin
        OraSession.Rollback;
        MessageDlg('#2 - ' + e.Message, mtWarning, [mbOK], 0);
      end;
      on e: Exception do
      begin
        OraSession.Rollback;
        MessageDlg('#3 - ' + e.Message, mtWarning, [mbOK], 0);
      end;
    else
      OraSession.Rollback;
      MessageDlg('#4 - An error occured.', mtWarning, [mbOK], 0); 
    end;
  finally
    if OraSession.InTransaction then
      OraSession.Commit;
  end;
Anybody had any luck catching integrity constraint issues in Oracle that can shed some light?

TIA!

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Sat 17 May 2008 07:42

We could not reproduce the problem. EOraError should be caught in such case.

Please specify the exact version of ODAC including build number, and whether you use ODAC Trial edition.
If it is possible, send to odac*crlab*com a complete small sample that demonstrates the problem, including the script for creating database objects.

Post Reply