Android Room数据库中如何将非空表列迁移为空
我是android room图书馆的新手。我需要将Not Null列迁移到Null, 但文件室迁移只允许在ALTER table查询中添加或重命名。如何执行列迁移查询Android Room数据库中如何将非空表列迁移为空,android,kotlin,android-room,androidx,Android,Kotlin,Android Room,Androidx,我是android room图书馆的新手。我需要将Not Null列迁移到Null, 但文件室迁移只允许在ALTER table查询中添加或重命名。如何执行列迁移查询 @Entity(tableName = "vehicle_detail") data class VehicleDetailsEntity( @PrimaryKey(autoGenerate = true) val vehicleClientId: Long = 0, val vehicleId: Stri
@Entity(tableName = "vehicle_detail")
data class VehicleDetailsEntity(
@PrimaryKey(autoGenerate = true)
val vehicleClientId: Long = 0,
val vehicleId: String,
val updatedOn: Date,
val updatedBy: String
)
我需要将表结构更改为
@Entity(tableName = "vehicle_detail")
data class VehicleDetailsEntity(
@PrimaryKey(autoGenerate = true)
val vehicleClientId: Long = 0,
val vehicleId: String,
val updatedOn: Date?,
val updatedBy: String?
)
java.lang.IllegalStateException:文件室无法验证数据完整性。看起来您已更改架构,但忘记更新版本号。您只需增加版本号即可解决此问题。您需要运行迁移,因为SQLite不允许修改列约束 对于该迁移,您需要创建一个新的临时表并将以前的所有数据复制到该表中,然后删除旧表并将临时表重命名为所需的表名 如果您有一个scheme目录,您可以找到您应该在迁移时复制的确切创建SQL查询(我刚刚从我的一个scheme中计算出来,可能不是100%正确): 可能重复的
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
// Create the new table
database.execSQL(
"CREATE TABLE IF NOT EXISTS VehicleDetailsEntityTmp (vehicleId TEXT NOT NULL, updatedOn TEXT, updatedBy TEXT,vehicleClientId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL )"
)
// Copy the data
database.execSQL(
"INSERT INTO VehicleDetailsEntityTmp (vehicleId, updatedOn, updatedBy ,vehicleClientId) SELECT vehicleId, updatedOn, updatedBy ,vehicleClientId FROM VehicleDetailsEntity ")
// Remove the old table
database.execSQL("DROP TABLE VehicleDetailsEntity")
// Change the table name to the correct one
database.execSQL("ALTER TABLE VehicleDetailsEntityTmp RENAME TO VehicleDetailsEntity")
}
}