I use SQL Developer (with Oracle XE) to set 3 tables and add PK,FK.. now I want to add some rows...
INSERT INTO "ADMIN"."POST" (NAME_POST) VALUES ('asd')
ORA-02291: integrity constraint (ADMIN.POST_DEPARTMENT_FK1) violated - parent key not found
ORA-06512: at line 1
I don't know where I can find this FK
to choose one! With SQL Developer, I only fill the columns without FK, so I need help to find how to do it right using SQLDeveloper
.
New information:
I don't know why, but I have a one-to-one relation between DEPARTMENT and POST.
CREATE TABLE "ADMIN"."POST"
( "ID_POST" NUMBER(*,0) NOT NULL ENABLE,
"NAME_POST" VARCHAR2(40 BYTE) NOT NULL ENABLE,
CONSTRAINT "POST_PK" PRIMARY KEY ("ID_POST")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ENABLE,
CONSTRAINT "POST_DEPARTMENT_FK1" FOREIGN KEY ("ID_POST")
REFERENCES "ADMIN"."DEPARTMENT" ("ID_DEPARTMENT") ON DELETE CASCADE ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ;
CREATE OR REPLACE TRIGGER "ADMIN"."POST_ID"
BEFORE INSERT ON POST
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:new.ID_POST is null) then
SELECT S_POST.nextval
INTO :new.ID_POST
FROM dual;
end if;
END;
/
ALTER TRIGGER "ADMIN"."POST_ID" ENABLE;
and post have FK CONSTRAINT "POST_DEPARTMENT_FK1" FOREIGN KEY ("ID_POST") REFERENCES "ADMIN"."DEPARTMENT" ("ID_DEPARTMENT") ON DELETE CASCADE ENABLE
Well, there's your problem. You've got a id_post
field which is - hopefully - the primary key of the post
table, and the trigger supports that. But you've also made the same column a foreign key to the department table. By doing that you have created the one-to-one relationship. That cannot work - the trigger is assigning a new post_id
value from its sequence, and it would be mere fluke if the department
table had a PK with the same value.
Assuming your post
table also has a id_department
column, the FK should be:
CONSTRAINT "POST_DEPARTMENT_FK1"
FOREIGN KEY ("ID_DEPARTMENT")
REFERENCES "ADMIN"."DEPARTMENT" ("ID_DEPARTMENT")
ON DELETE CASCADE ENABLE
It sounds like you're also saying that SQL Developer isn't showing you the FK column, department_id
, in the data editor. The only way I can see that being the case is if you opened the table view before you added that column via an alter
command. The table view doesn't refresh automatically if the table definition changes. Close the table and re-open it, and you'll see the current structure, including the FK column.
OK, from your question edit, you do not have a column to hold the department_id
that you want to be the FK. You need to include that column, and make it the FK:
CREATE TABLE "ADMIN"."POST"
( "ID_POST" NUMBER(*,0) NOT NULL ENABLE,
"NAME_POST" VARCHAR2(40 BYTE) NOT NULL ENABLE,
"ID_DEPARTMENT" NUMBER(*,0) NOT NULL,
CONSTRAINT "POST_PK" PRIMARY KEY ("ID_POST")
USING INDEX ...
TABLESPACE "SYSTEM" ENABLE,
CONSTRAINT "POST_DEPARTMENT_FK1" FOREIGN KEY ("ID_DEPARTMENT")
REFERENCES "ADMIN"."DEPARTMENT" ("ID_DEPARTMENT")
ON DELETE CASCADE ENABLE
) ...
)
TABLESPACE "SYSTEM" ;
Not a good idea to put your own objects in the SYSTEM
tablespace though; create a new one for your ADMIN
objects.
If you have no data yet it will be easiest to drop and recreate the table. Otherwise you can use alter table
commands to add the column and drop and recreate the constraint.