Nightloewe
Nightloewe

Reputation: 1098

Spring JPA does not detect deleted tables

I deleted my tables to let them be recreated by Spring JPA, but spring does not create them. Instead, I'm getting the following exception:

Unable to create unique key constraint (guild_id, setting_key) on table guild_setting: database column 'guild_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)

I don't know why I get this error, but before I deleted the tables in the database, the column name was guild_id, so what JPA says is not right.

This is an excerpt of the Entity:

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"guild_id", "setting_key"}))
public class GuildSetting extends Setting {

    @Column(nullable = false)
    private long guildId;

The following properties are set with the spring.datasource properties:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

Upvotes: 0

Views: 88

Answers (1)

Dirk Deyne
Dirk Deyne

Reputation: 6936

If you don't want to use @Column(name="guild_id")

You should use @UniqueConstraint(columnNames={"guildId", ...}

The generated table will contain the (correct) column, named guild_id

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"guildId", "setting_key"}))
public class GuildSetting extends Setting {

    @Column(nullable = false)
    private long guildId;

Agreed, this looks like a bug...


Note: You did not mention you have problems with the setting_key unique constraint.

Do you use @Column(name="setting_key") or private String setting_key?

Upvotes: 1

Related Questions