无法将字段设置为主键,因为fieldin Class中存在重复值

时间:2018-04-17 13:00:27

标签: java android realm

我无法理解,为什么我收到这条消息。我创建了领域数据库,我在其中有primaryKey表。在我的迁移中,我将字段设置为primaryKey。但是,当我的用户更新或重新安装时,应用程序会崩溃。

我不明白这个错误是什么意思。当我在手机中启动应用程序时,它成功运行。

MyMigration中的

public class TasksMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    final RealmSchema schema = realm.getSchema();
    if (oldVersion == 0) {
        oldVersion++;
    }
    if (oldVersion == 1) {
     if (!schema.contains("GoodsModelDB")) {
            schema.create("GoodsModelDB")
                    .addField("idGoodsPK", String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
                    .addField("id", long.class)
                    .addField("name", String.class)
                    .addField("note", String.class)
                    .addField("code", String.class)
                    .addField("model", String.class)
                    .addField("category", String.class)
                    .addField("type", String.class)
                    .addField("count", int.class);
        }

     final RealmObjectSchema goodsModelDB = schema.get("GoodsModelDB");
        if (goodsModelDB != null) {
            if (!goodsModelDB.getFieldNames().contains("idGoodsPK"))
                goodsModelDB.addField("idGoodsPK", String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED);
            if (!goodsModelDB.getFieldNames().contains("id"))
                goodsModelDB.addField("id", long.class);
            if (!goodsModelDB.getFieldNames().contains("name"))
                goodsModelDB.addField("name", String.class);
            if (!goodsModelDB.getFieldNames().contains("note"))
                goodsModelDB.addField("note", String.class);
            if (!goodsModelDB.getFieldNames().contains("code"))
                goodsModelDB.addField("code", String.class);
            if (!goodsModelDB.getFieldNames().contains("model"))
                goodsModelDB.addField("model", String.class);
            if (!goodsModelDB.getFieldNames().contains("category"))
                goodsModelDB.addField("category", String.class);
            if (!goodsModelDB.getFieldNames().contains("type"))
                goodsModelDB.addField("type", String.class);
            if (!goodsModelDB.getFieldNames().contains("count"))
                goodsModelDB.addField("count", int.class);

        }
    oldVersion++;
    }

在我的RealmObject

public class GoodsModelDB extends RealmObject implements Serializable {
@PrimaryKey
@Required
String idGoodsPK;
private long id;
private String name;
private String note;
private String code;
private String model;
private String category;
private String type;
private double price;
private boolean isActivated;
private long idSalePoint;
private long agentId;
private Date dateActivated;
private Date bindDate;
private double arpu;
private String pkOfSalePoint;
private boolean consign;
private long visitId;
private String idVisitByPK;
private boolean withoutException;
private boolean hasChanged;


public GoodsModelDB() {
}}

在PlayMarket中更新我的应用后,我在布料

中重现了这个错误
    Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.mitapp.dist_android/ru.mitapp.dist_android.screen.preloade.PreloadeActivity}: java.lang.IllegalArgumentException: Field 'idGoodsPK' cannot be set as primary key since there are duplicated values for field 'idGoodsPK' in Class 'GoodsModelDB'.
   (/Users/Nabil/Dev/realm/master/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsObjectStore.cpp:74)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
   at android.app.ActivityThread.access$900(ActivityThread.java:153)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5441)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

0 个答案:

没有答案