我的数据库类文件中有某个字段。我需要将他们的类型从String更改为int。我在github中按照示例进行操作。以下是我结束它的场景
public class LeaderBoardDb extends RealmObject {
// private String percentile;
//
// private String rank;
private String details;
private String email;
@PrimaryKey
private String userId;
private String name;
// private String rewards;
//
// private String sweatPointEarned;
private String photoUrl;
private String note;
private String displayName;
private float userPercentile;
private int userRank;
private int userRewards;
private int userSweatPointsEarned;
}
迁移代码
if(oldVersion == 6){
schema.get("LeaderBoardDb")
.addField("userPercentile", float.class)
.addField("userRank",int.class)
.addField("userRewards",int.class)
.addField("userSweatPointsEarned", int.class)
.removeField("percentile")
.removeField("rank")
.removeField("rewards")
.removeField("sweatPointEarned")
.renameField("userPercentile", "percentile")
.renameField("userRank", "rank")
.renameField("userRewards","rewards")
.renameField("userSweatPointsEarned", "sweatPointEarned");
oldVersion++;
}
Stack Stack我得到的内容如下:
03-31 13:52:03.901 9535-9535/com.workoutguru.workoutcash E/AndroidRuntime: Process: com.workoutguru.workoutcash, PID: 9535
03-31 13:52:03.901 9535-9535/com.workoutguru.workoutcash E/AndroidRuntime: io.realm.exceptions.RealmMigrationNeededException: Primary key not defined for field 'userId' in existing Realm file. Add @PrimaryKey.
03-31 13:52:03.901 9535-9535/com.workoutguru.workoutcash E/AndroidRuntime: at io.realm.LeaderBoardDbRealmProxy.validateTable(LeaderBoardDbRealmProxy.java:337)
03-31 13:52:03.901 9535-9535/com.workoutguru.workoutcash E/AndroidRuntime: at io.realm.DefaultRealmModuleMediator.validateTable(DefaultRealmModuleMediator.java:121)