Android房间迁移null错误

时间:2017-11-29 21:30:25

标签: android sqlite android-room android-architecture-components

当我在Android上从旧的sqlite方式迁移到Room时,我需要使用“INTEGER NOT NULL”进行编译。 问题是,当迁移发生时,你在新表中插入NULL字段,带有“NOT NULL”参数,我收到错误

android.database.sqlite.SQLiteConstraintException:NOT NULL约束失败:note.notification_state(code 1299)

编辑:

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)

代码:

`@Database(entities = {Note.class},version = 3) 公共抽象类NoteDatabase扩展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");

    }
};`

2 个答案:

答案 0 :(得分:1)

好的,我得到它的工作,如果有人有同样的问题,只使用NOT NULL DEFAULT 0(或其他数字)。

@Database(entities = {Note.class}, version = 3)

公共抽象类NoteDatabase扩展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");

    }
};

答案 1 :(得分:0)

迁移代码:

class Migration1To2: Migration(1, 2){
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE 'table_name' ADD COLUMN 'row_to modify' INTEGER NOT NULL **DEFAULT 1"**)
    }
}

 Dao Entity add **defaultValue = "1"**:
 @ColumnInfo( name="row_to modify", defaultValue = "1")
    var row_to modify: Int