Room Database Migration doesnt properly handle ALTER TABLE migration

后端 未结 7 2048
渐次进展
渐次进展 2020-12-13 06:36

Java.lang.IllegalStateException

Migration didn\'t properly handle user(therealandroid.github.com.roomcore.java.User).

Expected:

TableInfo{n

7条回答
  •  没有蜡笔的小新
    2020-12-13 06:50

    If you are getting notNull differences, you can simply mark your class field with @NonNull annotation, or change your sql with ALTER TABLE. But if you are getting column type differences, such as expected: TYPE=TEXT, then found TYPE='' (COLLATE NOCASE), or expected INTEGER, found INT, then the only solution is to drop and recreate your table. Sqlite does not allow changing column types.

    Use INTEGER in Sqlite instead of INT and mark your Java entity with @ColumnInfo(collate = NOCASE) (if you use NOCASE in Sqlite).

    Take a look at the json file under app\schemas to get the sql for the expected queries.

    static final Migration MIGRATION_2_3= new Migration(2, 3) {
            @Override
            public void migrate(SupportSQLiteDatabase database) {
    
                database.execSQL("DROP TABLE IF EXISTS table_tmp");
    
                database.execSQL("CREATE TABLE IF NOT EXISTS `table_tmp` ...");
    
                database.execSQL("insert into table_tmp (`id`, `name` , ...");
    
                database.execSQL("DROP INDEX IF EXISTS `index_table_name`");
    
                database.execSQL("CREATE INDEX IF NOT EXISTS `index_table_name` ON `table_tmp` (`name`)");
    
                database.execSQL("DROP TABLE IF EXISTS table");
    
                database.execSQL("alter table table_tmp rename to table");
    
            }
        };
    

提交回复
热议问题