Manually add @Required to the primary field to maintain the same behavior as 0.88.3 (upgrade to 0.89.0)

时间:2016-04-25 09:20:51

标签: android realm

I just updated realm from 0.88.3 to 0.89.0. In the changelog it says

PrimaryKey fields are no longer automatically marked as @Required. They can now be null if the type of the field can usually be null. This change will throw a RealmMigrationNeededException. Either manually add @Required to the primary field to maintain the same behavior as 0.88.3 and below, or change the nullability in a migration step.

What I tried:

1. Add @Required to the object but it throws "Error:(542, 114) error: incompatible types: String cannot be converted to long"

public class Migration{
   .
   .
   . 
   schema.get("Retailer")
      .setRequired("id", true);
   .
   .
}

@RealmClass
public class Retailer extends RealmObject {

    @PrimaryKey @Required
    private String id;
    .
    .
    .
}

How to do it correct?

1 个答案:

答案 0 :(得分:0)

感谢beeender,我更新到了更新的版本0.89.1,它现在正在运行。

只需将@Required添加到现有主键即可,无需迁移。

@RealmClass
public class Retailer extends RealmObject {

    @PrimaryKey @Required
    private String id;
}

如果向项目添加新的RealmObject,则必须编写以下迁移:

schema.create("Retailer")
    .addField("id", String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)