Subject Re: [firebird-support] What is My Foreign Key Mistake?
Author Helen Borrie
At 08:51 PM 16/12/2007, you wrote:
>Hi Folks:
>
> Developing C++ on Windows XP MediaCenter with VC6. Using
>Firebird Super Server 2.0.1 and IBPP 2.5.3.1. The Firebird
>server software is running on the same machine as the
>application.
>
> I'm getting this error message:
>
> "violation of FOREIGN KEY constraint "INTEG_31" on table "INC"
> Foreign key reference target does not exist"
>
> There is also a complaint about a deadlock, and Firebird seems
>to wait for about 10 seconds to report the problem. So, I'm not
>sure what's going on.
>
> The event table, with the foreign key, is created like this:
>
>CREATE TABLE EV
>(
> EV_KEY BIGINT NOT NULL,
>
> .
> .
> .
>
> PRIMARY KEY
> (
> EV_KEY
> )
>);
>
> I've simplified the INC table, that has the reference
>to the foreign key. This is the whole CREATE TABLE SQL:
>
>CREATE TABLE INC
>(
> INC_KEY BIGINT NOT NULL,
> INC_EVENT BIGINT NOT NULL REFERENCES EV(EV_KEY),
> PRIMARY KEY
> (
> INC_KEY
> )
>);
>
> I open a dialog and fill in data for EV table, then
>open a dialog and fill in data for the INC table.
>
> After returning from the INC dialog, and saying OK to
>the EV dialog, a record is added to the EV table, it's
>primary key is recorded, and the record is added to the
>INC table, with the EV_KEY matching the record just added
>to the EV table.
>
> Here is an entry being added to the EV table:
>
>INSERT INTO EV(
> EV_KEY,
>
> .
> .
> .
>
>)
>values
>(
> '2',
>
> .
> .
> .
>
>)
>
> Single stepping shows the INSERT INTO EV statement being
>prepared and executed without problems.
>
> Here is the error, with the whole message and the
>offending SQL:
>
>Message: isc_dsql_execute2 failed
>
>SQL Message : -913
>deadlock
>
>Engine Code : 335544336
>Engine Message :
>deadlock
>violation of FOREIGN KEY constraint "INTEG_31" on table "INC"
>Foreign key reference target does not exist
>
>Error for this SQL statement
>
>INSERT INTO INC(
> INC_KEY,
> INC_EVENT
>)
>values
>(
> '2',
> '2'
>)
>
> These are being pulled from a log file that records
>each transaction, when debugging, and the event table
>has a row inserted with an EV_KEY of '2'.
>
> I've stepped through the insertion the entry for the
>EV table seems to be processed just fine, without throwing
>any exceptions.
>
> As mentioned earlier, the debugger shows a pause of about
>10 seconds after the INC table's st->Execute(), which is an
>IBPP statement.
>
> What am I doing wrong?

It looks as though your two dialogs are in different transactions, with the child record in a concurrency transaction. The child record's transaction doesn't know of the existence of the key value it is meant to associate with.

./heLen