2016-03-22 7 views
2

値が、これは容易ではないと、私は知らないなぜレルムの移行重複は私が前に他の人がやったの移行(レルム87.2)をやりたい

私previsoly Categoriy.class

private int code; 
private String title; 
private String category; 
private int order; 
private boolean visible; 

@PrimaryKey @Index private String id; 
    private int code; 
    private String title; 
    private String category; 
    private int order; 
    private boolean visible; 

とMigration.class

で新しいCategory.classいくつかのダムの理由で0
public class Migration.... 
int i = 0; 
..... 
    if(oldVersion == 7) { 
      schema.get("Category").addField("id", String.class, FieldAttribute.PRIMARY_KEY).transform(new RealmObjectSchema.Function() { 
       @Override 
       public void apply(DynamicRealmObject obj) { 
        obj.set("id", UtilsForAll.getRandomUUID()); //Get Random UUID for previsoly added categories 
        obj.set("order", i); 
        i++; 
       } 
      }); 
oldVersion++; 

} 
} //finishClass 

と私はイムが間違っているのか知っている、それは私にこのエラーを与えておくいけない:

java.lang.IllegalArgumentException: Illegal Argument: Field "id" cannot be a primary key, it already contains duplicate values:

WHAT?カテゴリーのクラスに重複した値はありません。

PSは:これはちょうど私が実際に移行を使用するときに発生し、HAS PREVIOSLY CATEGORYのRECORDS

--------------------- EDIT 22/03

Emanuelez`s提案として、この試み:

if(oldVersion == 7) { 
schema.get("Category").addField("id", String.class); 
       schema.get("Category").transform(new RealmObjectSchema.Function() { 
        @Override 
        public void apply(DynamicRealmObject obj) { 
         obj.set("id", UtilsForAll.getRandomUUID()); //Get Random UUID for previsoly added categories 

        } 
       }); 
    oldVersion++; 

schema.get("Category").setPrimaryKey("id"); 
    } 

をエラーはこの1つになって -

io.realm.exceptions.RealmMigrationNeededException: Field 'id' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'id' or migrate using io.realm.internal.Table.convertColumnToNotNullable().

iがve tried .setNullable(「ID」、真の); `

何もすべて、これらのフィールドをデフォルト値に初期化されますので、あなただけの属性を持つフィールドを追加することはできません新しいプライマリキーフィールドを追加するにはあまりにも

答えて

3

を変更しませんすべてが等しく、これにより主キーの契約が破られます。 主キーの新しいフィールドを作成し、新たな別個の値

  • で新しいフィールドを移入
  • 新しいフィールドを作成

    1. :これにあなたがする必要がある正しい方法を行うには

      さらなる参考資料として、RealmObjectSchemaクラスの単体テストをご覧ください。here

  • +0

    この3つのステップを試してみましたが、何も変更されていません。ただエラーです。あなたが私に指摘したテストとまったく同じことを行っていると思います=( – user2582318

    +0

    私はまだこの問題に直面しています。 – AndoAiron

    関連する問題