如何将 Not Null table 列迁移到 Android Room 数据库中的 Null
How to Migrate Not Null table column into Null in Android Room database
我是 android 房间图书馆的新手。我需要将 Not Null 列迁移到 Null,
但是房间迁移只允许在 ALTER table 查询中添加或重命名。如何执行列迁移查询?
@Entity(tableName = "vehicle_detail")
data class VehicleDetailsEntity(
@PrimaryKey(autoGenerate = true)
val vehicleClientId: Long = 0,
val vehicleId: String,
val updatedOn: Date,
val updatedBy: String
)
我需要将 table 结构更改为
@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: Room 无法验证数据完整性。看起来您已更改架构但忘记更新版本号。您可以简单地通过增加版本号来解决这个问题。
您需要 运行 迁移,因为 SQLite 不允许修改列约束。
对于该迁移,您需要创建一个新的临时文件 table 并将所有以前的数据复制到其中,然后删除旧的 table 并将临时文件重命名为所需的 table 名字.
如果你有一个方案目录,你可以找到你应该在迁移时复制的确切创建 SQL 查询(我只是从我的方案中找出来的,不可能 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")
}
}
我是 android 房间图书馆的新手。我需要将 Not Null 列迁移到 Null, 但是房间迁移只允许在 ALTER table 查询中添加或重命名。如何执行列迁移查询?
@Entity(tableName = "vehicle_detail")
data class VehicleDetailsEntity(
@PrimaryKey(autoGenerate = true)
val vehicleClientId: Long = 0,
val vehicleId: String,
val updatedOn: Date,
val updatedBy: String
)
我需要将 table 结构更改为
@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: Room 无法验证数据完整性。看起来您已更改架构但忘记更新版本号。您可以简单地通过增加版本号来解决这个问题。
您需要 运行 迁移,因为 SQLite 不允许修改列约束。
对于该迁移,您需要创建一个新的临时文件 table 并将所有以前的数据复制到其中,然后删除旧的 table 并将临时文件重命名为所需的 table 名字.
如果你有一个方案目录,你可以找到你应该在迁移时复制的确切创建 SQL 查询(我只是从我的方案中找出来的,不可能 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")
}
}