簡體   English   中英

Room:java.lang.IllegalStateException:現有數據庫的遷移不正確

[英]Room: java.lang.IllegalStateException: Migration didn't properly for existing Database

使用的房間版本: - 1.1.1-rc1

我有一個由ormlite實現的現有數據庫。 我正在嘗試使用ormlite生成的現有sqlite文件遷移到Room。

當我們在ormlite中對table列使用long數據類型時,它會在sqlite中被轉換為BIGINT。 所以創建的模式包含BIGINT類型的列,它是房間的未知類型。當我嘗試使用Room升級應用程序時,我將獲得遷移異常。

java.lang.IllegalStateException:遷移沒有正確

Process: com.sample.playground, PID: 5587
java.lang.IllegalStateException: Migration didn't properly handle SampleReports(com.sample.playground.model.SampleReport
 Expected:
TableInfo{name='SampleReports', columns={timeslot=Column{name='timeslot', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, imeisvSvn=Column{name='imeisvSvn', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='SampleReports', columns={environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timeslot=Column{name='timeslot', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
    at com.sample.playground.model.SampleReport.SampleReportRoomDbHelper_Impl$1.validateMigration(SampleReportRoomDbHelper_Impl.java:77)
    at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:87)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:133)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
    at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
    at com.sample.playground.model.SampleReportDao_Impl.getReports(SampleReportDao_Impl.java:300)

使用BigInteger嘗試使用TypeConverter仍然無法正常工作。

我認為你需要通過重寫migrate方法來相應地轉換表,以便它 - 根據預期的列創建一個中間表, - 將數據復制到中間表, - 刪除原始表,然后 - 更改中間名的名稱表格為原始名稱

例如基於: -

DROP TABLE IF EXISTS SampleReports_intermediate;
CREATE TABLE IF NOT EXISTS SampleReports_intermediate (
    timeslot INTEGER NOT NULL, 
    environment TEXT NOT NULL,
    timestamp INTEGER NOT NULL,
    `trigger` INTEGER NOT NULL,
    imeisvSvn TEXT,
    _id INTEGER PRIMARY KEY
);
INSERT INTO SampleReports_intermediate (environment, timeslot, `trigger`, timestamp, _id)
    SELECT environment, timeslot,`trigger`, timestamp, _id FROM SampleReports
;
DROP TABLE IF EXISTS SampleReports;
ALTER TABLE SampleReports_intermediate RENAME TO SampleReports;

如果您沒有使用1.1室,請嘗試使用它,因為:

1.1版增加了對不在Room中的Sqlite類型的支持

Room Database Migration無法正確處理轉換

看看這個答案https://stackoverflow.com/a/52127819/7433710此錯誤是由於數據類型不匹配或最常見的Integer not null未在先前的Sqlite表中給出而您嘗試通過添加int變量進行初始化。 確保將NOT NULL添加到所有int列。

使用以下鏈接檢查我使用logcat輸出獲得的差異。 沒有足夠的聲譽發布圖像:(

使用logcat輸出進行比較

完整形象的差異

暫無
暫無

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

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