【问题标题】:Getting error Room cannot verify the data integrity... even after adding fallbackToDestructiveMigration()出现错误 Room 无法验证数据完整性......即使添加了 fallbackToDestructiveMigration()
【发布时间】:2018-11-20 05:54:27
【问题描述】:

为什么我在 Android 中添加 .fallbackToDestructiveMigration() 后仍收到错误 Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

private fun buildDatabase(context: Context): AppDatabase {
        val appDatabase = Room.databaseBuilder(context.applicationContext,
                AppDatabase::class.java,
                DATABASE_NAME
        )
        if (BuildConfig.DEBUG) {
            appDatabase.fallbackToDestructiveMigration()
        }
        return appDatabase.build()
    }

【问题讨论】:

    标签: android kotlin android-room


    【解决方案1】:

    我想你忘了增加数据库版本。更新数据库架构后,您必须增加数据库版本

    @Database(
            entities = [SampleEntity::class],
            version = 1
    )
    
    abstract class AppDatabase : RoomDatabase() {
    }
    

    【讨论】:

    • 有必要吗?我认为 fallbackToDestructiveMigration() 会删除旧数据库并创建一个新数据库,但如果我们需要升级数据库版本,它就像是数据库迁移的一种解决方法。如果我错了,请纠正我。
    • Realm DB 确实有一个类似的标志,它会丢弃旧的数据库并在需要时创建一个新的。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2021-08-07
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多