在Room中,必须从2迁移到1

时间:2018-05-15 06:59:19

标签: android database-migration android-room

我有一个应用程序,可以通过Room创建数据库。然后我将数据库移动到服务器进行填充。在更新或初始化应用程序时,应用程序会下载填充的数据库。但是当我想在Room使用它时,我收到一条错误消息:

  

需要从2迁移到1。请提供迁移   构建器或调用构建器中的fallbackToDestructiveMigration   在哪种情况下,房间将重新创建所有表格。

我清理了整个构建文件夹然后我再次完成了所有操作。但我又得到了同样的错误!

我得到的错误是:

String SALE_DATABASE_NAME = "SaleDatabase.db";
SaleDatabase saleDatabase = Room.databaseBuilder(this,
        SaleDatabase.class, SALE_DATABASE_NAME)
        //.fallbackToDestructiveMigration()    
        .allowMainThreadQueries()
        .build();
saleDatabase.getPathDao().getPaths(); //Getting error

数据库类:

@Database(entities = {OrderEntity.class, OrderDetailEntity.class
        , CardIndexDetailEntity.class, CardIndexEntity.class
        , CategoryEntity.class, CodingEntity.class
        , CustomerBasicEntity.class, CustomerBuyEntity.class
        , CustomerChequeEntity.class, CustomerCreditEntity.class
        , PathEntity.class, UnvisitedCustomerReasonEntity.class
        , ProfileCategoryEntity.class, SubCategoryDetailEntity.class
        , SubCategoryEntity.class, ReasonEntity.class}, version = 1)
public abstract class SaleDatabase extends RoomDatabase{

    public abstract PathDao getPathDao();

    @Override
    protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
        return null;
    }

    @Override
    protected InvalidationTracker createInvalidationTracker() {
        return null;
    }
}

我在数据库和SaleDatabase_Impl.java类中的哈希码都是一样的。

所以我的问题是:

  1. 为什么将迁移2比1称为降级工作?
  2. 在完全构建应用程序之前,如何忽略版本
  3. 如果我将版本更新为2,有没有办法清理和重置房间?

1 个答案:

答案 0 :(得分:0)

我这样解决:

  1. 清除应用数据
  2. 在清单集中android:allowBackup="false"