When i'm doing migration on Android from old sqlite way to Room i need to use "INTEGER NOT NULL" to compile. The problem is that when migration is happening you are inserting NULL field in the new table with "NOT NULL" parameters and I'm getting error
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: note.notification_state (code 1299)
Edit:
11-29 22:52:58.891 14605-14630/com.aleksandarvasilevski.notes E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
Process: com.aleksandarvasilevski.notes, PID: 14605
java.lang.IllegalStateException: Migration didn't properly handle note(com.aleksandarvasilevski.notes.repository.db.Note).
Expected:
TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=true, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=true, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=false, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
at com.aleksandarvasilevski.notes.repository.db.NoteDatabase_Impl$1.validateMigration(NoteDatabase_Impl.java:70)
at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:75)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:118)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:193)
at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:195)
at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:181)
at android.arch.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:87)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Code:
`@Database(entities = {Note.class}, version = 3) public abstract class NoteDatabase extends RoomDatabase {
public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL(
"CREATE TABLE note (id INTEGER NOT NULL, title TEXT, description TEXT, created_date TEXT, notification_date TEXT, notification_state INTEGER, priority INTEGER, PRIMARY KEY(id))");
database.execSQL(
"INSERT INTO note (id, title, description, created_date) SELECT _ID, title, description, date FROM notes");
}
};`
Ok i got it working, if anyone have the same problem just use NOT NULL DEFAULT 0(or other number).
@Database(entities = {Note.class}, version = 3)
public abstract class NoteDatabase extends RoomDatabase {
public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL(
"CREATE TABLE note (id INTEGER NOT NULL, " +
"title TEXT, description TEXT, created_date TEXT, notification_date TEXT, " +
"notification_state INTEGER NOT NULL DEFAULT 0, " +
"priority INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(id))");
database.execSQL(
"INSERT INTO note (id, title, description, created_date) " +
"SELECT _ID, title, description, date FROM notes");
}
};
// ...
}