
#Postgres foreign key constraint update
Here is the entire SQL code to create the necessary relations: CREATE TABLE Courses(ĬONSTRAINT Course_fk FOREIGN KEY(id) REFERENCES Courses(id) on delete cascade on update cascade,ĬONSTRAINT CourseDetailPK PRIMARY KEY (id)ĬONSTRAINT CourseDetail_fk FOREIGN KEY(id) REFERENCES Course_Detail(id) on delete cascade on update cascade,ĬONSTRAINT CourseContentPK PRIMARY KEY (id)ĬONSTRAINT CourseContent_fk FOREIGN KEY(id) REFERENCES Course_Content(id) on delete cascade on update cascade, The problem is with the second INSERT statement - I cannot use the id. INSERT INTO Course_Content(section_title) values ('A pretty awesome crash course') Įrror ERROR: insert or update on table "course_content" violates foreign key constraint "coursedetail_fk" Detail: Key (id)=(1) is not present in table "course_detail". SELECT currval(pg_get_serial_sequence('Courses','id')) I have also tried INSERT INTO Courses(title, description, lessons, duration) VALUES ('A crash course','An awesome crash course','33','4 Hours') INSERT INTO Course_Content(id, section_title) values (id, 'A pretty awesome crash course') INSERT INTO Courses(title, description, lessons, duration) VALUES ('A crash course','An awesome crash course','33','4 Hours') RETURNING id What I need is for the id of the inserted course to be returned so I can insert it into the Course_Content entity.

It has been a while since I wrote queries by hand, and I am having trouble inserting data into a relational OneToOne entity.
