I had a model that for example looks like this:
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Map;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
public class MyTable {
@Id
long myId;
@NotNull
String myField_1;
@ElementCollection(fetch = FetchType.EAGER)
@NonNull
Map<Integer, BigDecimal> myField_2;
}
I ran it on the server, the app (a Spring Boot application) worked with this table for a while and I decided that I need another field:
@ElementCollection(fetch = FetchType.EAGER)
@NonNull
Map<Integer, BigDecimal> myAdditionalField;
But when I run the app on the server again I get such exceptions:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table my_table_my_additional_field add constraint FK35gbk1v4oaclmxu6onjiopln0 foreign key (my_table_my_id) references my_table (my_id)" via JDBC Statement
...
Caused by: java.sql.SQLException: Can't create table `my_database_name`.`#sql-7f7_4531` (errno: 150 "Foreign key constraint is incorrectly formed")
Why does it happening and how to add new fields to my entity? I have a property jpa.hibernate.ddl-auto: update
. The funniest part is that the field and it's "map collection" are added to the db correctly, it's just there is this strange constraint error for this new field
Search for that error message.
Here's one answer that lists some options: https://stackoverflow.com/a/56976063/1766831
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.