Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot add a NOT NULL column with default value NULL" with Requery after automatic DB migration

Tags:

android

I updated my AbstractFooEntity class by adding an integer field like below, and I bumped the DB version (the DB is initialized with new DatabaseSource(context, Models.DEFAULT, DB_VERSION).

@Entity
abstract class AbstractFooEntity {
  // this was in DB schema v1
  String someField;

  // added in DB schema v2
  int newField = 0;
}

When I deploy this code and the (automatic) DB migration is performed when the user runs the new version of the Android app, I get the following error at runtime: "Cannot add a NOT NULL column with default value NULL".

What's the proper way to annotate the entity so that the framework correctly handles the automatic DB migration in this scenario?

like image 826
jakub.g Avatar asked Feb 19 '18 17:02

jakub.g


Video Answer


1 Answers

I found a solution of this.

database.execSQL("ALTER TABLE table_name ADD COLUMN colum_name INTEGER DEFAULT 1 not null");

add the command : "DEFAUT value" after data type will solve your problem.

like image 77
Tăng Tấn Tài Avatar answered Oct 24 '22 18:10

Tăng Tấn Tài