使用现有布尔列类型进行房间迁移 [英] room migration using existing boolean column types

查看:18
本文介绍了使用现有布尔列类型进行房间迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我发现了什么

所有@entity 注释的类都在编译时处理,并生成数据库类的实现.然后在访问数据库之前,调用这个生成的类的 validateMigration 方法.这个 validateMigration 方法通过原始查询与现有的数据库模式进行验证

All the @entity annotated classes are processed during compiletime and an Implementation for Database class is generated. Then before accessing the db, validateMigration method of this generated class is called. This validateMigration method verifies with the existing db schema via raw query

PRAGMA table_info mytable 名称

(参见 android.arch.persistence.room.util.TableInfo.java 的 L208)

问题来了

我的 sqlite3 db 有一些列类型为 BOOLEAN 的列.(其中 slqite 在内部处理为 int).现在,当我创建房间实体时说

My sqlite3 db has some columns with column type as BOOLEAN. (which slqite internally handles to int). Now when i create room entities say

public someEntity {
     @columnInfo(name="someName")
     public Boolean myValue;
}

房间的创建表查询将是

创建表 someEntity ( myValue INTEGER)

当我们使用 PRAGMA table_info someEntity 查询现有 db 时,我们得到

Where as when we query the existing db with PRAGMA table_info someEntity we get

1|myValue|BOOLEAN|0||0

如上所述,room 通过比较字段名称、列类型等来验证(从 sqlite 到 room)迁移.由于列类型不匹配(BOOLEAN 和 INTEGER),因此会引发错误,提示迁移失败.

As explained above room verifies the ( sqlite to room ) migration by comparing field name, column type etc. And since the column types dont match (BOOLEAN and INTEGER) it throws an error saying migration failed.

任何人都可以提出解决方法吗?我们可以在 sqlite 中腾出空间创建 BOOLEAN 列类型吗?(同样,我们无法更改/更改现有表的列类型.)

Can anyone suggest a workaround to this ? Can we make room create BOOLEAN column type in sqlite ? (Also afaik we can't change/alter column types of existing tables.)

PS:我也看到 VARCHAR 的类似问题 - 使用现有的 VARCHAR带房间的列

PS: I also see a similar issue with VARCHAR - Using an existing VARCHAR column with Room

推荐答案

使用 DEFAULT 值和 NOT 定义新属性 newAttribute 的迁移NULL.

Define the migration for the new attribute newAttribute with both a DEFAULT value and as NOT NULL.

代码

database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")

完整代码

@Database(entities = arrayOf(ModelName::class), version = 2)
@TypeConverters(Converters::class)
abstract class DatabaseName : RoomDatabase() {

    abstract fun daoName(): DaoName

    companion object {

        private var INSTANCE: DatabaseName? = null

        fun getAppDatabase(context: Context): DatabaseName {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.applicationContext,
                        DatabaseName::class.java, DATABASE_NAME)
                        .addMigrations(MIGRATION_1_2)
                        .build()
            }
            return INSTANCE as DatabaseName
        }

        val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")
            }
        }
    }

}

这篇关于使用现有布尔列类型进行房间迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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