DB模式1对象看起来像这样:
open class LicenseAgreementEntity(
@PrimaryKey
var licenseAgreementID: String = UUID.randomUUID().toString(),
var version: String = "",
var acceptAgreementDate: String = "",
var acceptAgreementStatus: String = AcceptAgreementStatus.ACCEPTED.name
) : RealmObject()
在数据库模式2中,对象将如下所示:
open class LicenseAgreementEntity(
@PrimaryKey
var accountID: String = UUID.randomUUID().toString(),
var version: String = "",
var acceptAgreementDate: String = "",
var acceptAgreementStatus: String = AcceptAgreementStatus.ACCEPTED.name
) : RealmObject()
我的迁移代码如下
realm?.let { transactionRealm ->
val schema = transactionRealm.schema
val licenseAgreementSchema = schema.get(TABLE_LICENSE_AGREEMENT_ENTITY) ?: return
licenseAgreementSchema
.renameField(FIELD_LICENSE_AGREEMENT_ID, FIELD_ACCOUNT_ID)
.transform {
it.set(FIELD_ACCOUNT_ID, preference.accountID)
}
}
问题是在迁移到其他值期间我需要更新该字段。我尝试了添加/删除字段和添加/删除主键的不同变体,但看起来变换操作正在破坏它。关于如何重命名主键字段并在迁移中更改其值的任何想法?
例外:
Caused by: java.lang.IllegalArgumentException: Primary key field 'accountID' cannot be changed after object was created.