Subject Re: [firebird-support] Foreign key reference target does not exist. [FB 2.5 beta 2]
Author Helen Borrie
At 09:46 AM 21/09/2009, you wrote:
>hello,
>I tried to create a foreign key but I always get this error:
>
>violation of FOREIGN KEY constraint "".
>violation of FOREIGN KEY constraint "FK_RES_TREEVIEW_1" on table "RES_TREEVIEW".
>Foreign key reference target does not exist.

It means what it says. The FK cascade rule SET DEFAULT is failing because there is no record in the REFERENCES table with the default value.

Make sure that each step is committed before passing to the next one. If you're running all this stuff in a script, make sure you include SET AUTO ON before running the script; and also know that DML (the required INSERT INTO STYLES..... statement) will *not* be committed by AUTODDL and must be explicitly committed before passing the ALTER TABLE statement that attempts to define the FK constraint.

./heLen



>here is my two tables DDL:
>
>CREATE TABLE STYLES (
> STYLE_ID INTEGER DEFAULT -1 NOT NULL,
> STYLE_NAME VARCHAR(80) NOT NULL,
> BACK_COLOR VARCHAR(35),
> BACK_COLOR_ENABLED SMALLINT DEFAULT 0,
> FONT_STYLE_IS_UNDER SMALLINT DEFAULT 0,
> FONT_STYLE_IS_STRIKE SMALLINT DEFAULT 0,
> BITMAP_IMAGE BLOB SUB_TYPE 0 SEGMENT SIZE 80
>);
>
>ALTER TABLE STYLES ADD CONSTRAINT PK_STYLES1 PRIMARY KEY (STYLE_ID);
>
>===============================================================
>
>CREATE TABLE RES_TREEVIEW (
> FOLDER_ID BIGINT NOT NULL,
> FOLDER_PARENT_ID BIGINT NOT NULL,
> FOLDER_NAME VARCHAR(100) NOT NULL,
> FOLDER_POSITION BIGINT NOT NULL,
> FOLDER_IMAGE_INDEX SMALLINT,
> FOLDER_FLAG BLOB SUB_TYPE 0 SEGMENT SIZE 80,
> FOLDER_ICON BLOB SUB_TYPE 0 SEGMENT SIZE 80,
> IS_FOLDER_CUSTOM_ICON SMALLINT DEFAULT 0,
> STYLE_ID INTEGER DEFAULT -1
>);
>
>ALTER TABLE RES_TREEVIEW ADD CONSTRAINT PK_RES_TREEVIEW PRIMARY KEY (FOLDER_ID);
>
>alter table RES_TREEVIEW
>add constraint FK_RES_TREEVIEW_1
>foreign key (STYLE_ID)
>references STYLES(STYLE_ID)
>on delete SET DEFAULT
>on update CASCADE
>
>========================================================
>any advice ?
>
>Thanks
>
>
>
>------------------------------------
>
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>Visit http://www.firebirdsql.org and click the Resources item
>on the main (top) menu. Try Knowledgebase and FAQ links !
>
>Also search the knowledgebases at http://www.ibphoenix.com
>
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>Yahoo! Groups Links
>
>
>