我一直在尝试重命名字段或向我的领域数据库添加新字段。但是我遇到这样的错误。我尝试调试我的应用,看来oldVersion ++没有增加旧的架构版本。详细错误位于下方
任何帮助将不胜感激。
io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
Property 'TransferDetailModel.MainUniqueId' has been made optional.
open class Migration : RealmMigration {
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
var oldVersion = oldVersion
val schema = realm.schema
if (oldVersion == 0L) {
val tdmSchema = schema.get("TransferDetailModel")
tdmSchema!!
.addField("MainUniqueId", String::class.java, FieldAttribute.REQUIRED)
.transform { obj -> obj.set("MainUniqueId", obj.getString("DetailUniqueId"))}
.removeField("DetailUniqueId")
oldVersion++
}
}
}
`
open class BaseApplication: Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
var mConfiguration = RealmConfiguration.Builder()
.name("BSOMSDB.realm")
.schemaVersion(1)
.migration(Migration())
.build()
Realm.setDefaultConfiguration(mConfiguration)
val realm = Realm.getDefaultInstance()
}
}
lateinit var realm: Realm
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_transfer_main)
Realm.init(this)
realm = Realm.getDefaultInstance() // Getting error at this line
@RealmClass
open class TransferDetailModel: RealmObject() {
@PrimaryKey
private var DetailId:Long = 0
private var MainId:Long = 0
private var MainUniqueId: String = ""
private var Barcode:String = ""
private var Quantity:Int = 0
private lateinit var InsertDate: Date
fun setMainUniqueId(MainUniqueId:String)
{
this.MainUniqueId=MainUniqueId
}
fun setDetailId(DetailId:Long)
{
this.DetailId=DetailId
}
fun setMainId(MainId:Long)
{
this.MainId=MainId
}
fun setBarcode(Barcode:String)
{
this.Barcode=Barcode
}
fun setQuantity(Quantity:Int)
{
this.Quantity=Quantity
}
fun setInsertDate(InsertDate:Date)
{
this.InsertDate=InsertDate
}
fun getDetailId():Long
{
return DetailId
}
fun getMainId():Long
{
return MainId
}
fun getMainUniqueId():String
{
return MainUniqueId
}
fun getBarcode():String
{
return Barcode
}
fun getQuantity():Int
{
return Quantity
}
fun getInsertDate():Date
{
return InsertDate
}
}
09-09 23:17:49.687 32090-32090/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bambiEnt.BambiSOMS, PID: 32090
java.lang.RuntimeException: Unable to create application com.bambiEnt.BambiSOMS.classes.BaseApplication: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'TransferDetailModel.MainUniqueId' has been made optional.
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4966)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5737)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'TransferDetailModel.MainUniqueId' has been made optional.
at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:171)
at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241)
at io.realm.BaseRealm.<init>(BaseRealm.java:136)
at io.realm.BaseRealm.<init>(BaseRealm.java:103)
at io.realm.Realm.<init>(Realm.java:163)
at io.realm.Realm.createInstance(Realm.java:499)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:355)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:285)
at io.realm.Realm.getDefaultInstance(Realm.java:407)
at com.bambiEnt.BambiSOMS.classes.BaseApplication.onCreate(BaseApplication.kt:21)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4963)
... 8 more
答案 0 :(得分:0)
您在FieldAttribute.REQUIRED
中添加了“ MainUniqueId”。但是在您的Realmclass中
没有用@Required
如果需要“ MainUniqueId”,则需要添加@Required
注释。但在迁移时,不需要.addField("MainUniqueId", String::class.java).setNullable("MainUniqueId", true)
将其设置为可选注释。
答案 1 :(得分:0)
Realm.init(this)
设置了默认的RealmConfiguration,仅应在Application.onCreate
内部调用。
默认配置会覆盖您的RealmConfiguration,这就是为什么迁移不会运行的原因。