【问题标题】:Not able to drop models in realm migration无法在领域迁移中删除模型
【发布时间】:2022-06-14 10:13:34
【问题描述】:

我想从以前的架构中删除对象模型 所以我在迁移中添加了这些行。

 schema.remove("StringsTranslationRealm");
 schema.remove("OptionApiRealm");

但我遇到了这个异常。

: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'OptionApiRealm' has been added.
- Class 'StringsTranslationRealm' has been added.
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7173)
    at android.app.ActivityThread.access$1800(ActivityThread.java:284)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2266)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:233)
    at android.os.Looper.loop(Looper.java:334)
    at android.app.ActivityThread.main(ActivityThread.java:8333)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)

我正在删除这些(表格)而不是添加它们。搜索了一下,没找到解决办法

【问题讨论】:

  • 编辑:已解决。我没有从代码中删除 OptionApiRealm 和 StringTranslationRealm 类(领域对象)。所以这些是在我在迁移中删除它们之后添加的

标签: android migration realm


【解决方案1】:

添加 deleteRealmIfMigrationNeeded: true 如果您只是想尝试领域并避免此 RealmMigrationNeededException: Migration is required 错误,仅在开发时执行此操作,因为如果您更改架构,您的数据库将被删除,在生产中始终使用迁移

  const realm = await Realm.open({
    deleteRealmIfMigrationNeeded: true,
    schema: [OrderSchema, OrderDetailSchema],
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多