android - 房间持久性库 : Weird Error during migration

标签 android database-migration android-room

这个错误让我抓狂。到目前为止我找不到任何答案。我有旧数据库,我正在将其迁移到 Persistence Room 库。但是,每当我进行迁移时,都会出现以下错误,

java.lang.IllegalStateException: Migration didn't properly handle. 

我使用的代码如下:

@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
}

对于迁移,

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");
    }
};

当我运行程序时。我的 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=[]}

我不知道这个“INTEGER”来自哪里。我尝试卸载、使缓存无效以及任何其他密切相关的解决方案。知道发生了什么事吗?如果您刚安装应用程序,它工作得很好。仅在迁移后出现错误。根据我的日志猫,所有迁移步骤似乎都已完成,但它仍然显示迁移未正确处理。

最佳答案

谢谢大家的帮助,但经过几天令人沮丧的调试后,我终于找到了答案。在我的应用程序 list 中,自动备份已开启,

android:allowBackup="true"

因此,在尝试各种数据库时,谷歌实际上会备份我所有新创建的数据库及其结构,并在我重新安装应用程序时自动恢复它们。因此我得到了这个有线错误。一旦我关闭自动备份 android:allowBackup="false" 并重新安装应用程序,我就可以正确测试迁移。

我建议所有开发者以后遇到这样的问题,开发时SWITCH OFF自动备份。您可以在测试迁移后将其打开。

关于android - 房间持久性库 : Weird Error during migration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46912727/

相关文章:

javascript - Android CSS spritesheet webkit 转换和动画的问题

java - 如何在 Activity 中获取全局变量(运行时错误)

java - 我可以为每个 fragment 单独提供工具栏吗?如何处理抽屉导航

Postgresql 8.4->9.1,pg_upgrade 失败,因为数据库 'template0' 存在

android - RecyclerView 项目。从多个来源获取数据

kotlin - get() 和 by lazy 之间的区别

android - 将 JKS 文件转换为 BKS 或创建 BKS 文件

MySQL -> 带有大表问题的 SQL Server 迁移助手

ruby-on-rails - 部署到 Heroku "ERROR: must be superuser to COPY to or from a file"

android-room - Jetpack Compose - 如何在 LazyColumn 中搜索并显示房间中的特定数据?