Issue
Good day, I suppose
I was learning spring boot and working on small project to expand my knowledge.
Somehow I got exception while launching spring boot app, that I don't know how to solve.
Here is the part of exception message
Hibernate: alter table token add constraint FK79keudebybjlldk2o4i0nwqev foreign key (user_user_id) references user
2022-05-18 12:01:20.531 WARN 9092 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "alter table token add constraint FK79keudebybjlldk2o4i0nwqev foreign key (user_user_id) references user" via JDBC Statement
I don't know exactly where is the trouble so here is the link to repository. As far as I know everything was expected to work safe and sound ;(
Solution
Spring boot probably was configured to have spring.jpa.hibernate.ddl-auto=update
meaning it will update the schema to match the domain layer of spring application when needed.
Considering that the schema already contained some data (table records), Spring tried to update a table to have a constraint, but that constraint was not respected by the existing data so the DDL failed.
Cleaning the data of the mentioned table would probably allow spring to execute the DDL script to apply the aforementioned constraint.
Otherwise you can switch to spring.jpa.hibernate.ddl-auto=create
where Spring will first remove all tables from database which would mean removal of existing data as well, and then recreate the schema. This would have as effect that no previous data would violate the constraint. Hence spring will be able to move forward with DDL.
Answered By - Panagiotis Bougioukos
Answer Checked By - Marie Seifert (JavaFixing Admin)