我正在尝试从Sqlite迁移到Room但我收到迁移错误,我相信这是因为我使用了VARCHAR()(我遵循了一个指南,绝对希望我会使用TEXT,但是现在无法解决这个问题。
这是我的实体:
@Entity(tableName = "conversation")
class Conversation(@PrimaryKey
@ColumnInfo(name = "key") val key: Int,
@ColumnInfo(name = "message_id") val messageId: String?,
@ColumnInfo(name = "thread_id") val threadId: String?,
@ColumnInfo(name = "address") val address: String?,
@ColumnInfo(name = "group_address") val groupAddress: String?,
@ColumnInfo(name = "name") val name: String?,
@ColumnInfo(name = "group_name") val groupName: String?,
@ColumnInfo(name = "body") val body: String?)
以下是我收到的错误:
Expected:
TableInfo{name='conversation', columns={date=Column{name='date', type='INTEGER', notNull=false, primaryKeyPosition=0}, address=Column{name='address', type='TEXT', notNull=false, primaryKeyPosition=0}.....
Found:
TableInfo{name='conversation', columns={date=Column{name='date', type='INTEGER', notNull=false, primaryKeyPosition=0}, address=Column{name='address', type='VARCHAR(14)', notNull=false, primaryKeyPosition=0}....
它只是一大堆,我不确定如何将VARCHAR转换为文本或类似的东西
编辑:迁移方法
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(TexpertDatabase.CONVO_CREATE) //Creates table named conversation_temp with preferred schema
database.execSQL("INSERT INTO conversation_temp SELECT ${TexpertDatabase.allCols} FROM conversation")
database.execSQL("DROP TABLE conversation")
database.execSQL("ALTER TABLE conversation_temp RENAME TO conversation")
}
答案 0 :(得分:2)
通常,关系数据库提供ALTER TABLE
命令,允许您更改列的数据类型。但遗憾的是,SQLite提供very limited ALTER TABLE
support,仅涉及:
因此,标准(也许只是)这样做的方法是
所有这些代码都将出现在您将提供给房间的迁移的migrate
功能中。
答案 1 :(得分:2)
Room 1.1增加了对不在Room中的Sqlite类型的支持,以便修复除主键之外的所有问题。最终,这是一个解决问题的迁移,花了很多工作但是这里的代码最终成功了,如果有其他人有这个问题:
override fun migrate(database: SupportSQLiteDatabase) {
//Creates table named conversation_temp with copy of table from before, and adds a PRIMARY KEY AUTO INCREMENT to that
database.execSQL(TexpertDatabase.CONVO_CREATE)
database.execSQL("INSERT (column, names, listed) INTO conversation_temp SELECT * FROM conversation")
database.execSQL("DROP TABLE conversation")
database.execSQL("ALTER TABLE conversation_temp RENAME TO conversation")
}