房间自动迁移无此列:column_name(code 1 SQLITE_ERROR):,编译时:插入

roqulrg3  于 2023-10-23  发布在  SQLite
关注(0)|答案(2)|浏览(143)

我尝试通过添加新字段来更新我的房间实体:
版本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,

但我也有同样的错误
我做错了什么?一般来说,我只需要迁移一个不改变的表,其余的可以删除,但我没有找到这样做的方法,所以我使用自动迁移。但这不起作用。任何解决办法都对我有好处,先谢谢你了

mw3dktmi

mw3dktmi1#

对于任何可能遇到同样问题的人,

观察结果:如果您在递增数据库版本之前构建应用程序,则可能会发生这种情况,当room尝试执行迁移时,源JSON模式将被解释为与目标JSON模式相同
解决方案:修改源JSON模式以匹配旧版本,然后重新构建,或者如果您将其置于版本控制之下,则只需重置为提交的版本
注意:JSON schema是指定义你的数据库schema的房间自动生成的JSON文件

yqkkidmi

yqkkidmi2#

@Remc4评论:* 同时检查您的版本1模式文件。如果它包含isExchangeSupported字段,则您可能在增加版本号之前运行了生成项目。*
他说的完全正确,这个项目是在我添加迁移之前构建的,所以它已经在1模式中包含了这个字段。
所以要解决这个问题

  • 删除以前创建的架构文件
  • 重建项目。

相关问题