Dex*_*ter 5 android database-migration android-room
我因这个错误而挠头。到目前为止我找不到任何答案。我有旧数据库,我正在将其迁移到 Persistence Room 库。但是,每当我进行迁移时,都会出现以下错误,
java.lang.IllegalStateException: Migration didn't properly handle.
Run Code Online (Sandbox Code Playgroud)
我使用的代码如下:
@Entity(tableName = ROUTE_TABLE)
public class RouteData {
static final String ROUTE_TABLE = "name";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@ColumnInfo(name = "col1")
private String col1;
//Other columns with exactly same as 'col1' just different names
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
对于迁移,
Room.databaseBuilder(context.getApplicationContext(), MyData.class, DATABASE_NAME)
.addMigrations(MIGRATION_LATEST)
.fallbackToDestructiveMigration()
.build();
private static final Migration MIGRATION_LATEST = new Migration(9, 10) {
@Override
public void migrate(SupportSQLiteDatabase db) {
//Log.i("Tag" , "Migration Started");
//Migration logic
//Log.i("Tag" , "Migration Ended");
}
};
Run Code Online (Sandbox Code Playgroud)
当我运行程序时。我的 LogCat 中出现“迁移结束”日志,但是当我再次尝试访问数据库时,出现以下错误。
Caused by: java.lang.IllegalStateException: Migration didn't properly handle xxx.
Expected:
TableInfo{name='name', columns={col1=Column{name='col1', type='TEXT', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}
Found:
TableInfo{name='name', columns={col1=Column{name='col1', type='INTEGER', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}
Run Code Online (Sandbox Code Playgroud)
我不知道这个“整数”从哪里来。我尝试卸载、使缓存无效和任何其他密切相关的解决方案。知道发生了什么吗?如果您新安装应用程序,它工作得很好。只有在迁移后才会出现错误。根据我的日志猫,所有迁移步骤似乎都已完成,但仍显示迁移未正确处理。
感谢大家试图帮助我,但是经过几天令人沮丧的调试后我终于找到了答案。在我的应用程序清单中,自动备份已打开,
机器人:allowBackup =“真”
因此,在尝试各种数据库时,谷歌实际上正在备份我新创建的任何数据库及其结构,并在我重新安装应用程序时自动恢复它们。因此我收到了这个有线错误。一旦我切换自动备份android:allowBackup="false"并重新安装应用程序,我就可以正确测试迁移。
我建议所有开发人员在将来遇到此类问题时,在开发时关闭自动备份。测试迁移后,您可以将其打开。
| 归档时间: |
|
| 查看次数: |
2612 次 |
| 最近记录: |