Android Room 迁移未正确处理(错误的列顺序)
Posted
技术标签:
【中文标题】Android Room 迁移未正确处理(错误的列顺序)【英文标题】:Android Room migration didn't properly handle (wrong columns order) 【发布时间】:2019-02-15 20:47:54 【问题描述】:我正在使用 Room 数据库进行迁移。我收到此错误:
java.lang.IllegalStateException: Migration didn't properly handle coffee_productivity(io.github.omisie11.coffeeproductivitytracker.database.entity.CoffeeProductivityData).
Expected:
TableInfoname='coffee_productivity', columns=date=Columnname='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, id=Columnname='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, productivity=Columnname='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, coffees_volume=Columnname='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, number_of_coffees=Columnname='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, foreignKeys=[], indices=[Indexname='index_coffee_productivity_date', unique=true, columns=[date]]
Found:
TableInfoname='coffee_productivity', columns=date=Columnname='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, productivity=Columnname='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, coffees_volume=Columnname='coffees_volume', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, number_of_coffees=Columnname='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, id=Columnname='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, foreignKeys=[], indices=[Indexname='index_coffee_productivity_date', unique=true, columns=[date]]
看起来列的顺序发生了变化,但我唯一做的就是添加一个新列。 实体:
@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)
数据库类:
@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
【问题讨论】:
【参考方案1】:预期:
coffees_volume=Columnname='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0
找到:
coffees_volume=Columnname='coffees_volume', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0
问题出在您的notNull
属性中。
用以下代码替换您的模型:
@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)
【讨论】:
哦,这才是真正的问题,谢谢你的帮助 :)【参考方案2】:请替换查询Like
ALTER TABLE 'coffee_productivity' ADD COLUMN 'coffees_volume' INTEGER NOT NULL DEFAULT 0
【讨论】:
【参考方案3】:发生的情况是,您通过添加字段或删除或更改名称和迁移时间来更改表会产生此问题,而没有将更改放入表中。但他希望他的桌子在正确的预期中,所以他发现与他的预期不同,我这样决定:
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")
在页尾教Room官网的原因:https://developer.android.com/training/data-storage/room/migrating-db-versions?hl=en#kotlin
【讨论】:
以上是关于Android Room 迁移未正确处理(错误的列顺序)的主要内容,如果未能解决你的问题,请参考以下文章
从 SQLite 迁移到 Android Room Persistence Library