簡體   English   中英

Android Room 遷移未正確處理(列順序錯誤)

[英]Android Room migration didn't properly handle (wrong columns order)

我正在使用 Room 數據庫進行遷移。 我收到此錯誤:

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]}]}

看起來列的順序發生了變化,但我所做的唯一一件事就是添加一個新列。 實體:

@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
        }
    }
}

預期的:

coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}

成立:

coffees_volume=Column{name='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)
}

請替換查詢喜歡

ALTER TABLE 'coffee_productivity' ADD COLUMN 'coffees_volume' INTEGER NOT NULL DEFAULT 0

發生的情況是,您通過添加字段或通過刪除或更改名稱和遷移時間來更改表會導致此問題,而未將更改放入表中。 但是他希望他的桌子在正確的期望中,所以他發現了他期望的不同,我是這樣決定的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM