领域迁移错误“已添加属性'x'”

时间:2018-07-04 09:37:52

标签: android realm realm-migration

我无法使用迁移领域

在我的项目中,您需要配置领域的迁移。我创建了一个迁移类,添加了迁移并向realmObject添加了字段,但无法正常工作

我收到此错误消息:

由于以下错误,需要迁移:

  • 已添加属性“ PickupState.latitude”。
  • 已添加属性“ PickupState.longitude”。

旧的realmObject模型

public class PickupState extends RealmObject {
  @PrimaryKey
  private long autoIncrementId;
  private long id;
  private String userName;
  private String eventType;
  private String eventData;
  private String syncStatus;
  private String syncErrorCode;

  public void setAutoIncrementId(long l) {
    autoIncrementId = l;
  }

  public long getAutoIncrementId() {
    return autoIncrementId;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(State eventType) {
    this.eventType = eventType.name();
  }

  public String getEventData() {
    return eventData;
  }

  public void setEventData(String eventData) {
    this.eventData = eventData;
  }

  public String getSyncStatus() {
    return syncStatus;
  }

  public void setSyncStatus(String syncStatus) {
    this.syncStatus = syncStatus;
  }

  public String getSyncErrorCode() {
    return syncErrorCode;
  }

  public void setSyncErrorCode(String syncErrorCode) {
    this.syncErrorCode = syncErrorCode;
  }
}

新的realmObject

public class PickupState extends RealmObject {
  @PrimaryKey
  private long autoIncrementId;
  private long id;
  private String userName;
  private String eventType;
  private String eventData;
  private String syncStatus;
  private String syncErrorCode;
  private double latitude;
  private double longitude;

  public void setAutoIncrementId(long l) {
    autoIncrementId = l;
  }

  public long getAutoIncrementId() {
    return autoIncrementId;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(State eventType) {
    this.eventType = eventType.name();
  }

  public String getEventData() {
    return eventData;
  }

  public void setEventData(String eventData) {
    this.eventData = eventData;
  }

  public String getSyncStatus() {
    return syncStatus;
  }

  public void setSyncStatus(String syncStatus) {
    this.syncStatus = syncStatus;
  }

  public String getSyncErrorCode() {
    return syncErrorCode;
  }

  public void setSyncErrorCode(String syncErrorCode) {
    this.syncErrorCode = syncErrorCode;
  }

  public double getLatitude() {
    return latitude;
  }

  public void setLatitude(double latitude) {
   this.latitude = latitude;
    }

   public double getLongitude() {
    return longitude;
  }

   public void setLongitude(double longitude) {
   this.longitude = longitude;
  }
}

迁移类:

public class PickupStateMigration implements RealmMigration {

  @Override
  public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
    final RealmObjectSchema schema = realm.getSchema().get("PickupState");
    assert schema != null;
    if (oldVersion == 0) {
        schema.addField("latitude", double.class);
        schema.addField("longitude", double.class);
      oldVersion++;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

要执行迁移,还需要更改模式版本。

RealmConfiguration config = new RealmConfiguration.Builder()
                                    .schemaVersion(1)
                                    .migration(new PickupStateMigration())
                                    // ...
                                    .build()

答案 1 :(得分:0)

我制作了两个表和两个不同的schemaVersion 但是我更改了一个版本并进行了迁移 现在,我更改了两个版本,并为它们进行了相同的迁移

我必须为每个表使用模块,但我没有这样做