Reputation: 1337
By mistake I sit the @Column annotation property to false while my intention was to set it to true.
@Column(nullable = false)
private String title;
I tried to change the property to true after the table was created but It doesn't get changed in my table. Even though I have the following lines in my application.properties file:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Now when I try to create a new record I gett this error:
SQL Error: 1048, SQLState: 23000
Column 'title' cannot be null
I know that I can change nullability manually through phpMyAdmin but I'm wondering why the column annotation property doesn't work after the table is created.
Upvotes: 0
Views: 807
Reputation: 8206
From: How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?
The update operation for example will attempt to add new columns, constraints, etc but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run.
In general, the schema generation tool is mostly for prototyping, testing and validation. Incremental scripts using dedicated tools are a better solution for production.
There is also a note about this in the documentation:
Although the automatic schema generation is very useful for testing and prototyping purposes, in a production environment, it’s much more flexible to manage the schema using incremental migration scripts.
I'm not overly familiar with these type of tools but, to give you an example, Liquibase is just the first one that comes to mind.
Upvotes: 1