Subject Re: Foreign key constraint violation
Author hfsnell
Thank for your reply!
My two table DDL:
Table 1:
CREATE TABLE OBJECT_LIST(
OBJECT_ID UVAR31 NOT NULL,
OTYPE_ID DMSMALLINT NOT NULL,
OBJGRP_ID UVAR15,
OACCOUNT_ID UVAR15,
OBJECT_NAME UVAR127,
OTHER_NAME UVAR127,
SUBINFO_1 UVAR127,
SUBINFO_2 UVAR63,
SUBINFO_3 UVAR127,
SUBINFO_4 UVAR127,
SUBINFO_5 UVAR127,
SUBINFO_6 UVAR127,
CREATE_DATE DATEDM,
OTHER_DATE DATEDM,
OBJECT_VALUE DMNUMBER,
OBJECT_NOTES UVAR127,
OBJECT_STATUS DMSMALLINT,
DEBT_LEN DMSMALLINT,
MAKH UVAR31,
LOAIKH DMSMALLINT,
LASTUSER_MODIFY UVAR15,
DISCOUNT_DEF DMNUMBER,
LAST_MODIFY DATETIMEDM,
OBJECT_HIDE DMSMALLINT,
MUCGIA DMSMALLINT,
TONKHOTT DMINTEGER,
MS_PPXK DMSMALLINT,
DEFAULT_UNIT UVAR15 COLLATE UNICODE_FSS,
CONSTRAINT PK_OBJECT_LIST PRIMARY KEY (OBJECT_ID,OTYPE_ID)
);
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Loai doi tuong'
where RDB$FIELD_NAME = 'OTYPE_ID' and RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ten doi tuong, tenn
' where RDB$FIELD_NAME = 'OBJECT_NAME' and RDB$RELATION_NAME =
'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ten khac, ten ngan
gon...' where RDB$FIELD_NAME = 'OTHER_NAME' and RDB$RELATION_NAME =
'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Dia chi, qui cach
mat hang,...' where RDB$FIELD_NAME = 'SUBINFO_1' and
RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Don vi tinh' where
RDB$FIELD_NAME = 'SUBINFO_2' and RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ghi chu ve doi tuong
' where RDB$FIELD_NAME = 'OBJECT_NOTES' and RDB$RELATION_NAME =
'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Trang thai doi
tuong' where RDB$FIELD_NAME = 'OBJECT_STATUS' and RDB$RELATION_NAME =
'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Thoi han cong no, so
ngay phai thanh toan khi co phat sinh' where RDB$FIELD_NAME =
'DEBT_LEN' and RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ma so khach hang neu
a don hang
Loa
Ma doi tuong tham chieu' where RDB$FIELD_NAME = 'MAKH' and
RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Loai kh neu la don
hang, dai ly, luon=1 or null
Loai doi tuong tham chieu' where RDB$FIELD_NAME = 'LOAIKH' and
RDB$RELATION_NAME = 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ton kho toi thieu
cho hang hoa' where RDB$FIELD_NAME = 'TONKHOTT' and RDB$RELATION_NAME
= 'OBJECT_LIST';
UPDATE RDB$RELATION_FIELDS set RDB$DESCRIPTION = 'Ma so xuat kho cua
hang hoa' where RDB$FIELD_NAME = 'MS_PPXK' and RDB$RELATION_NAME =
'OBJECT_LIST';
ALTER TABLE OBJECT_LIST ADD CONSTRAINT FK_OBJECT_LIST_ACC
FOREIGN KEY (OACCOUNT_ID) REFERENCES ACCOUNT_LIST (ACCOUNT_ID) ON
UPDATE CASCADE;
ALTER TABLE OBJECT_LIST ADD CONSTRAINT FK_OBJECT_LIST_GRP
FOREIGN KEY (OBJGRP_ID,OTYPE_ID) REFERENCES OBJECT_GROUP
(OBJGRP_ID,OTYPE_ID) ON UPDATE CASCADE;
ALTER TABLE OBJECT_LIST ADD CONSTRAINT FK_OBJECT_LIST_KH
FOREIGN KEY (MAKH,LOAIKH) REFERENCES OBJECT_LIST
(OBJECT_ID,OTYPE_ID) ON UPDATE CASCADE;
ALTER TABLE OBJECT_LIST ADD CONSTRAINT FK_OBJECT_LIST_TYPE
FOREIGN KEY (OTYPE_ID) REFERENCES OBJECT_TYPE (OTYPE_ID) ON UPDATE
CASCADE;
ALTER TABLE OBJECT_LIST ADD CONSTRAINT FK_OBJECT_LIST_UNIT
FOREIGN KEY (DEFAULT_UNIT) REFERENCES UNIT (UNIT_ID) ON UPDATE CASCADE;
GRANT DELETE, INSERT, REFERENCES, SELECT, UPDATE
ON OBJECT_LIST TO "SYSDBA" WITH GRANT OPTION;
Table 2:
CREATE TABLE OBJECT_BAL(
OBJECT_ID UVAR31 NOT NULL,
OTYPE_ID DMSMALLINT NOT NULL,
ACCOUNT_ID UVAR15 NOT NULL,
PERIOD_ID DMSMALLINT NOT NULL,
BEGIN_DEBIT DMNUMBER,
BEGIN_CREDIT DMNUMBER,
DEBIT_AMOUNT DMNUMBER,
CREDIT_AMOUNT DMNUMBER,
CONSTRAINT PK_OBJECT_BAL PRIMARY KEY
(OBJECT_ID,PERIOD_ID,ACCOUNT_ID,OTYPE_ID)
);
ALTER TABLE OBJECT_BAL ADD CONSTRAINT FK_OBJECT_BAL_1
FOREIGN KEY (OBJECT_ID,OTYPE_ID) REFERENCES OBJECT_LIST
(OBJECT_ID,OTYPE_ID) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE OBJECT_BAL ADD CONSTRAINT FK_OBJECT_BAL_2
FOREIGN KEY (ACCOUNT_ID) REFERENCES ACCOUNT_LIST (ACCOUNT_ID) ON
UPDATE CASCADE ON DELETE CASCADE;
GRANT DELETE, INSERT, REFERENCES, SELECT, UPDATE
ON OBJECT_BAL TO "SYSDBA" WITH GRANT OPTION;
And the foreign constraint is FK_OBJECT_BAL_1
The action can be updating or inserting.

--- In firebird-support@yahoogroups.com, Kjell Rilbe <kjell.rilbe@...>
wrote:

> Personally, I'd need a lot more detalied and specific info to be
able to
> help.
>
> Isolate the two tables and the FK relation that you apparently violate.
>
> Give us the table defs and the FK constraint definition.
>
> Tell us what you (your application) is trying to insert or update.
>
> For a start...
>
> Kjell
> --
> --------------------------------------
> Kjell Rilbe
> DataDIA AB
> E-post: kjell@...
> Telefon: 08-761 06 55
> Mobil: 0733-44 24 64
>