I would like to change the data type of the Realm field from Stringto intand FYI, this field is also Primary Key. I could not find a method in RealmMigrationto solve this problem.
PS: My application is already ready, and all the values that are currently in this field are integers.
EDIT 1
My model class
public class Team extends RealmObject {
@SerializedName("id")
@PrimaryKey
private int id;
@SerializedName("name")
private String name;
@SerializedName("description")
private String description;
}
my migration after responding to the christian answer
if (oldVersion == 6) {
RealmObjectSchema teamSchema = schema.get("Team");
teamSchema.addField("temp_id", int.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("temp_id", Integer.valueOf(obj.getString("id")));
}
})
.removeField("id")
.renameField("temp_id", "id")
.addPrimaryKey("id");
}
source
share