Android Room,错误的列名和列不可为空

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

我现在使用Room而不是经典的sqlite语句,我遇到了以下问题。当我打开“标签”表时,我收到以下错误。

java.lang.IllegalStateException: Migration didn't properly handle tags(de.yochyo.ybooru.database.entities.Tag).
     Expected:
    TableInfo{name='tags', columns={name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, creation=Column{name='creation', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, isFavorite=Column{name='isFavorite', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
     Found:
    TableInfo{name='tags', columns={name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, Date=Column{name='Date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, isFavorite=Column{name='isFavorite', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
        at de.yochyo.ybooru.database.Database_Impl$1.validateMigration(Database_Impl.java:81)
        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:398)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
        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 de.yochyo.ybooru.database.entities.TagDao_Impl.getAllTags(TagDao_Impl.java:142)
        at de.yochyo.ybooru.database.Database$tags$1$job$1.invokeSuspend(Database.kt:41)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

预期和发现之间的差异是这个

Expected:
creation=Column{name='creation', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}
Found:
Date=Column{name='Date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}

这就是我的Tag类的样子。

@Entity(tableName = "tags")
class Tag(@PrimaryKey val name: String, val type: Int, var isFavorite: Boolean = false,@ColumnInfo(name = "creation") val creation: Date? = null)

有两个问题。列名(应该是“创建”)是日期,我的日期列不能包含null

有谁知道为什么会这样?

android kotlin database-migration android-room
1个回答
1
投票

修复:“预期”表示Room期望表包含那些列,“Found”是数据库文件中的列。

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