首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android迁移没有正确处理(列顺序错误)

Android迁移没有正确处理(列顺序错误)
EN

Stack Overflow用户
提问于 2018-09-11 10:59:28
回答 3查看 7K关注 0票数 9

我正在使用Room数据库进行迁移。我得到了这个错误:

代码语言:javascript
复制
java.lang.IllegalStateException: Migration didn't properly handle coffee_productivity(io.github.omisie11.coffeeproductivitytracker.database.entity.CoffeeProductivityData).
 Expected:
TableInfo{name='coffee_productivity', columns={date=Column{name='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, productivity=Column{name='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, number_of_coffees=Column{name='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='index_coffee_productivity_date', unique=true, columns=[date]}]}
 Found:
TableInfo{name='coffee_productivity', columns={date=Column{name='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, productivity=Column{name='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, number_of_coffees=Column{name='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}}, foreignKeys=[], indices=[Index{name='index_coffee_productivity_date', unique=true, columns=[date]}]}

看起来列的顺序发生了变化,但我所做的唯一的事情就是添加一个新列。实体:

代码语言:javascript
复制
@Entity(tableName = "coffee_productivity", indices = [Index(value = "date", unique = true)])
data class CoffeeProductivityData(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "date") var date: String,
@ColumnInfo(name = "productivity") var productivity: Int,
@ColumnInfo(name = "number_of_coffees") var numberOfCoffees: Int,
@ColumnInfo(name = "coffees_volume") var coffeesVolume: Int)
{
constructor() : this(null, "00/00/0000", 0, 0, 0)
}

数据库类:

代码语言:javascript
复制
@Database(entities = [CoffeeProductivityData::class], version = 2)
abstract class CoffeeProductivityDatabase : RoomDatabase() {

    abstract fun coffeeProductivityDao(): CoffeeProductivityDao

    companion object {
        private var dbInstance: CoffeeProductivityDatabase? = null

        private val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE 'coffee_productivity' ADD COLUMN 'coffees_volume' INTEGER DEFAULT 0")
            }
        }

        fun getDatabase(context: Context): CoffeeProductivityDatabase? {
            if (dbInstance == null) {

                dbInstance = Room.databaseBuilder<CoffeeProductivityDatabase>(
                        context.applicationContext,
                        CoffeeProductivityDatabase::class.java,"coffee_productivity.db")
                        .addMigrations(MIGRATION_1_2)
                        .build()
            }
            return dbInstance
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-11 12:04:32

预期:

代码语言:javascript
复制
coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}

找到:

代码语言:javascript
复制
coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}

问题在于您的notNull属性。用以下代码替换您的模型:

代码语言:javascript
复制
@Entity(tableName = "coffee_productivity", indices = [Index(value = "date", unique = true)])
data class CoffeeProductivityData(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "date") var date: String,
@ColumnInfo(name = "productivity") var productivity: Int,
@ColumnInfo(name = "number_of_coffees") var numberOfCoffees: Int,
@ColumnInfo(name = "coffees_volume") var coffeesVolume: Int? = 0)
{
   constructor() : this(null, "00/00/0000", 0, 0, 0)
}
票数 10
EN

Stack Overflow用户

发布于 2019-03-09 17:12:15

请将查询替换为

代码语言:javascript
复制
ALTER TABLE 'coffee_productivity' ADD COLUMN 'coffees_volume' INTEGER NOT NULL DEFAULT 0
票数 0
EN

Stack Overflow用户

发布于 2020-05-15 01:53:05

所发生的情况是,您通过添加字段或删除或更改迁移的名称和时间来更改表,这导致了这个问题,而没有将更改放在表中。但是他希望他的桌子是正确的,所以他发现了他所期望的不同,我就这样决定:

代码语言:javascript
复制
val MIGRATION_2_3 = object: Migration (2, 3) {
    override fun migrate (database: SupportSQLiteDatabase) {
        database.execSQL ("" "
                CREATE TABLE new_Song (
                    id INTEGER PRIMARY KEY NOT NULL,
                    name TEXT,
                    TEXT NOT NULL DEFAULT tag ''
                )
                "" ".trimIndent ())
        database.execSQL ("" "
                INSERT INTO new_Song (id, name, tag)
                SELECT id, name, tag FROM Song
                "" ".trimIndent ())
        database.execSQL ("DROP TABLE Song")
        database.execSQL ("ALTER TABLE new_Song RENAME TO Song")
    }
}

在页面的末尾,说明了“房间”官方网站的原因:https://developer.android.com/training/data-storage/room/migrating-db-versions?hl=en#kotlin

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52274366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档