由于 属性 id 已成为必需,因此需要迁移
Migration is required due to Property id has been made required
如果我将 Payment.kt 和 CartPayment.kt 中的 lateinit var id: String
更改为 var id: String? = ""
,它工作正常,但问题是我想要 id 是必需的,如何我能做到吗?
错误:
java.lang.RuntimeException: Unable to create application: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'CartPayment.id' has been made required.
- Property 'Payment.id' has been made required.
型号:
open class Payment() : RealmObject() {
@PrimaryKey
lateinit var id: String
var typeValue: Int = 0
var statusValue: Int = 0
var value: Double = 0.0
var referenceNumber: String? = null
注意:除了 class 名称
之外,付款和购物车付款模式完全相同
Migration.kt
class Migration : RealmMigration {
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
var oldVersion = oldVersion
val schema = realm.schema
if (oldVersion == 0L) {
schema.create("Payment")
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY)
.addField("typeValue", Int::class.java)
.addField("statusValue", Int::class.java)
.addField("value", Double::class.java)
.addField("referenceNumber", String::class.java)
schema.get("Order")!!
.addRealmListField("payments", schema.get("Payment")!!)
oldVersion++
}
if (oldVersion == 1L) {
schema.create("CartPayment")
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY)
.addField("typeValue", Int::class.java)
.addField("statusValue", Int::class.java)
.addField("value", Double::class.java)
.addField("referenceNumber", String::class.java)
schema.get("Order")!!
.addField("cashPaymentAmount", Float::class.java)
.addField("change", Float::class.java)
oldVersion++
}
}
}
App.kt
class App: Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
val realmConfig = RealmConfiguration.Builder()
.schemaVersion(2)
.migration(Migration())
.build()
Realm.getInstance(realmConfig)
Realm.setDefaultConfiguration(realmConfig)
Fresco.initialize(this)
}
}
基本上,您要添加一个新字段 "id",它是主键(因此是必填键)。
如果在初始化(lateinit)时没有指定任何值,领域将如何迁移所有没有 id 但在迁移后需要的早期记录?因此错误。
以下解决方案可能有效
- 要么预填充 id(不使用 lateinit)
- 将您之前的记录转换为具有 ID(如果它们没有)
查看官方示例here
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
成功了。
如果声明变量为lateinit,请务必添加FieldAttribute.REQUIRED
.
如果我将 Payment.kt 和 CartPayment.kt 中的 lateinit var id: String
更改为 var id: String? = ""
,它工作正常,但问题是我想要 id 是必需的,如何我能做到吗?
错误:
java.lang.RuntimeException: Unable to create application: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'CartPayment.id' has been made required.
- Property 'Payment.id' has been made required.
型号:
open class Payment() : RealmObject() {
@PrimaryKey
lateinit var id: String
var typeValue: Int = 0
var statusValue: Int = 0
var value: Double = 0.0
var referenceNumber: String? = null
注意:除了 class 名称
之外,付款和购物车付款模式完全相同Migration.kt
class Migration : RealmMigration {
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
var oldVersion = oldVersion
val schema = realm.schema
if (oldVersion == 0L) {
schema.create("Payment")
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY)
.addField("typeValue", Int::class.java)
.addField("statusValue", Int::class.java)
.addField("value", Double::class.java)
.addField("referenceNumber", String::class.java)
schema.get("Order")!!
.addRealmListField("payments", schema.get("Payment")!!)
oldVersion++
}
if (oldVersion == 1L) {
schema.create("CartPayment")
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY)
.addField("typeValue", Int::class.java)
.addField("statusValue", Int::class.java)
.addField("value", Double::class.java)
.addField("referenceNumber", String::class.java)
schema.get("Order")!!
.addField("cashPaymentAmount", Float::class.java)
.addField("change", Float::class.java)
oldVersion++
}
}
}
App.kt
class App: Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
val realmConfig = RealmConfiguration.Builder()
.schemaVersion(2)
.migration(Migration())
.build()
Realm.getInstance(realmConfig)
Realm.setDefaultConfiguration(realmConfig)
Fresco.initialize(this)
}
}
基本上,您要添加一个新字段 "id",它是主键(因此是必填键)。 如果在初始化(lateinit)时没有指定任何值,领域将如何迁移所有没有 id 但在迁移后需要的早期记录?因此错误。
以下解决方案可能有效
- 要么预填充 id(不使用 lateinit)
- 将您之前的记录转换为具有 ID(如果它们没有)
查看官方示例here
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
成功了。
如果声明变量为lateinit,请务必添加FieldAttribute.REQUIRED
.