Subject Re: [firebird-support] Foreign key violation - but why???
Author Lucas Franzen
Bjoern,

Bjoern Reimer schrieb:

> Hi,
>
> (Firebird 1.5.1 CS on Linux)
>
> Here is my situation:
>
> Two tables with one record each. I want to create a foreign key
> constraint from one to the other.
>
>
> CREATE TABLE XPERT_ANMELDUNGEN (
> ANMELDUNG_ID BIGINT NOT NULL,
> PRUEFUNGSART_ID INTEGER NOT NULL,
> PERSON_ID INTEGER NOT NULL,
> XP_TERM_ID INTEGER NOT NULL,
> ZAHLUNGSSTATUS SMALLINT DEFAULT 0 NOT NULL
> );

You have to define a primary key on your table(s).

CREATE TABLE XPERT_ANMELDUNGEN (
ANMELDUNG_ID BIGINT NOT NULL,
PRUEFUNGSART_ID INTEGER NOT NULL,
PERSON_ID INTEGER NOT NULL,
XP_TERM_ID INTEGER NOT NULL,
ZAHLUNGSSTATUS SMALLINT DEFAULT 0 NOT NULL,

/* ADD THIS LINE */
CONSTRAINT PK_XPERT_ANMELDUNGEN PRIMARY KEY ( ANMELDUNG_ID )
);

Otherwise your ForeignKey definition should fail with an error like:

unsuccessful metadata update
could not find UNIQUE INDEX with specified columns

Luc.