Hello together I am currently trying to persist an entity to the database with a 1 to 1 relationship.
I am using spring version 2.5.3 (spring-boot-data-jdbc). When I try to persist via rest post and the usual crud repository save() method with this json.
{
"name": "everyday at 15",
"announcement": {
"name": "This is the third announcement"
}
}
I get this error message:
2021-08-19 14:20:06.315 ERROR 7946 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.InsertRoot(entity=Timetable(id=null, name=everyday at 15, announcement=Announcement(id=null, name=This is the third announcement)))] with root cause
org.postgresql.util.PSQLException: ERROR: insert or update on table "timetable" violates foreign key constraint "fk_announcement"
Detail: Key (announcement)=(6) is not present in table "announcement".
It seems like the counter for the announcement id is always going up however it never reaches the point where anything is persisted. My entity and db setup are listed below.
@Table("announcement")
data class Announcement(
@Id
val announcement_id: Long?,
val name: String
)
@Table("timetable")
data class Timetable(
@Id
var id: Long?,
val name: String,
val announcement: Announcement
)
CREATE TABLE announcement(
announcement_id serial,
name varchar(30) not null,
PRIMARY KEY(id)
);
CREATE TABLE timetable(
id serial,
name varchar(30) not null,
announcement_id serial,
PRIMARY KEY(id),
CONSTRAINT fk_announcement
FOREIGN KEY (announcement_id)
REFERENCES announcement (announcement_id)
);
Thank you for your help!