Room Persistence Library:迁移过程中的奇怪错误 [英] Room Persistence Library: Weird Error during migration

查看:92
本文介绍了Room Persistence Library:迁移过程中的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am scratching my head with this error. I couldn't find any answer so far. I have old database which I am migrating to Persistence Room library. However whenever I do migration, I am getting following error,

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

我正在使用的代码如下:

Code I am using is as follows:

@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中收到迁移结束"日志,但是当我再次尝试访问数据库时,却出现以下错误.

When I run program. I get "Migration Ended" log in my LogCat but when I am trying access database again, it is giving me following error.

 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"要去哪里.我尝试卸载,使缓存和任何其他紧密相关的解决方案无效.知道发生了什么吗?如果您是刚安装应用程序,它的效果会很好.仅在迁移后才会出现错误.根据我的日志,所有迁移步骤似乎都已完成,但仍显示迁移未正确处理.

I have no idea where where this "INTEGER" is coming. I tried uninstalling, invalidating cache and any other closely related solution. Any idea what is going on? It works perfectly fine if you freshly install app. Error is coming up only after migration. According to my log cat, all migration steps seems to be completed but it is still showing migration not handled properly.

推荐答案

谢谢大家为我提供的帮助,但是经过几天令人沮丧的调试,我终于找到了答案.在我的应用清单中,自动备份功能已启用,

Thank you all for trying to help me, however I have finally found the answer after days of frustrating debugging. In my app manifest auto backup was on,

android:allowBackup ="true"

android:allowBackup="true"

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

So while trying out various databases, google was actually backing up my any newly created databases and their structure and restoring them automatically when I was reinstalling app. Hence I was getting this wired error. Once I switch of auto backup android:allowBackup="false" and reinstall app, I can test migration properly.

对于以后遇到此类问题的所有开发人员,我的建议是在开发过程中自动关闭自动备份.测试完迁移后即可将其打开.

My suggestion for all developers in future encountering such problem, SWITCH OFF auto backup while development. You can switch it on once you have tested your migration.

这篇关于Room Persistence Library:迁移过程中的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆