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

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

问题描述

我到目前为止发现的结果

所有@entity注释类都在编译期间进行处理,并生成了Database类的实现.然后,在访问数据库之前,将调用此生成的类的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 name

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

现在有问题

我的sqlite3数据库有一些列类型为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;
}

该会议室的创建表查询将为

The room's create table query will be

Create Table someEntity ( myValue INTEGER)

当我们用PRAGMA table_info someEntity查询现有数据库时,我们得到

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 NULL定义新属性 newAttribute 的迁移.

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天全站免登陆