我尝试通过添加新字段来更新我的 Room 实体:
版本1:
@Entity(tableName = TABLE_NAME)
data class AvailableAccountRoom(
@PrimaryKey
val id: String = "",
[...]
)
版本2:
@Entity(tableName = TABLE_NAME)
data class AvailableAccountRoom(
@PrimaryKey
val id: String = "",
[...]
val isExchangeSupported: Boolean,
)
我想使用自动迁移,所以我也将此行添加到我的数据库中:
autoMigrations = [
AutoMigration(from = 1, to = 2)
]
但是我在运行时崩溃并显示下一条消息:
(1) no such column: isExchangeSupported in "INSERT INTO `_new_table_name` (`id`,`isExchangeSupported`
(1) no such column: isExchangeSupported in "INSERT INTO `_new_table_name` (`id`,`isExchangeSupported`
FATAL EXCEPTION: OkHttp Dispatcher
Process: ...
android.database.sqlite.SQLiteException: no such column: isExchangeSupported (code 1 SQLITE_ERROR): , while compiling: INSERT INTO
`_new_table_name` (`id`,`isExchangeSupported`) SELECT
`id`,`isExchangeSupported` FROM
`table_name `
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1068)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:673)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:34)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2086)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2008)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.kt:246)
at com.###.AppDatabase_AutoMigration_1_2_Impl.migrate(AppDatabase_AutoMigration_1_2_Impl.java:18)
at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.kt:91)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.kt:253)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:416)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableOrReadableDatabase(FrameworkSQLiteOpenHelper.kt:232)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.innerGetDatabase(FrameworkSQLiteOpenHelper.kt:190)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getSupportDatabase(FrameworkSQLiteOpenHelper.kt:151)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.kt:104)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:638)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:457)
at androidx.room.RoomDatabase.query(RoomDatabase.kt:486)
at androidx.room.util.DBUtil.query(DBUtil.kt:75)
at com.###Dao_Impl$6.call(LoginTokenDao_Impl.java:156)
at com.###Dao_Impl$6.call(LoginTokenDao_Impl.java:153)
at androidx.room.CoroutinesRoom$Companion$execute$4$job$1.invokeSuspend(CoroutinesRoom.kt:88)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
我还尝试在实体中指定新字段的默认值:
val isExchangeSupported: Boolean = false,
但是我也有同样的错误
我做错了什么? 一般来说,我只需要迁移一个不改变的表,其余的可以删除,但我没有找到一种方法来做到这一点,所以我使用自动迁移。但这是行不通的。 任何解决方案都会对我有好处,提前谢谢
对于任何可能遇到同样问题的人,
观察:如果您在增加数据库版本之前构建应用程序,则可能会发生这种情况,当 room 然后尝试进行迁移时,源 JSON 模式将被解释为与目标 JSON 模式相同
解决方案:修改源 JSON 架构以匹配旧版本,然后重做构建,或者如果您将其置于版本控制之下,则只需重置为提交的版本
注意:通过 JSON 模式,我指的是由 room 定义数据库模式自动生成的 JSON 文件