Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to add a new boolean column to a table which already has data

Tags:

java

spring

jpa

I tried adding a new attribute to an entity Customer which already has data stored in the database:

private boolean isArchived;

This gives me the following error when i start spring and try to update the tables:

Error executing DDL "alter table customer add column is_archived boolean not null" via JDBC Statement

Is that because he tries to initialize isArchived with null for the already existing rows and the primitive type cant be null? Is there a way to just initialize them with false instead or do i have to use Boolean instead of boolean to allow null values(which i would like not to allow)?

like image 727
M.Dietz Avatar asked Dec 14 '25 02:12

M.Dietz


1 Answers

It's because existing rows can't have null values, with the new non-null column. The way to do is to provide a default value. You can do it like below:

@Column(nullable=false, columnDefinition = "BOOLEAN DEFAULT FALSE")
private boolean isArchived;
like image 131
Shafiul Avatar answered Dec 16 '25 15:12

Shafiul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!