Room:java.lang.IllegalStateException:现有数据库的迁移不正确

问题描述 投票:2回答:2

使用的房间版本: - 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仍然无法正常工作。

android android-sqlite android-room sqliteopenhelper
2个回答
0
投票

我认为你需要通过覆盖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 didn't properly handle conversion


0
投票

查看此答案https://stackoverflow.com/a/52127819/7433710此错误是由于数据类型不匹配或最常见的Integer not null而未在先前的Sqlite表中给出而您尝试通过添加int变量进行初始化。确保将NOT NULL添加到所有int列。

使用以下链接检查我使用logcat输出获得的差异。没有足够的声誉发布图像:(

Comparison using your logcat output

Full Image of the difference

© www.soinside.com 2019 - 2024. All rights reserved.