1

I am using Realm in my Android apps and I would like to do the following:

  • Make an existing field as Primary key.
  • Change the type of field from int to String

I wrote the following migration for the above changes:

public class MyMigration implements RealmMigration {
    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();
            RealmObjectSchema objectSchema = schema.get("AccessInfo");
            objectSchema.addIndex("bookId");
            objectSchema.addPrimaryKey("bookId");
            objectSchema.removeField("lastOpenedTimeStamp");
            objectSchema.addField("lastOpenedTimeStamp", String.class);
        oldVersion++;
    }
}

However I still face the following error in the MainActivity:

 Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'AccessInfo.bookId' has been made indexed.
- Property 'AccessInfo.lastOpenedTimeStamp' has been changed from 'int' to 'string'.
- Primary Key for class 'AccessInfo' has been added.

The following is my Realm config

defaultConfig = new RealmConfiguration.Builder().modules(new DefaultModule()).migration(new MyMigration()).build();
realm = Realm.getInstance(defaultConfig);

AccessInfo:

public class AccessInfo extends RealmObject {

    @PrimaryKey
    int bookId;
    String lastOpenedTimeStamp;
    int lastOpenedPageId;
    int lastOpenedPageNumber;
    int lastOpenedPartNumber;
    int accessCount;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getLastOpenedTimeStamp() {
        return lastOpenedTimeStamp;
    }

    public void setLastOpenedTimeStamp(String lastOpenedTimeStamp) {
        this.lastOpenedTimeStamp = lastOpenedTimeStamp;
    }

    public int getLastOpenedPageId() {
        return lastOpenedPageId;
    }

    public void setLastOpenedPageId(int lastOpenedPageId) {
        this.lastOpenedPageId = lastOpenedPageId;
    }

    public int getLastOpenedPageNumber() {
        return lastOpenedPageNumber;
    }

    public void setLastOpenedPageNumber(int lastOpenedPageNumber) {
        this.lastOpenedPageNumber = lastOpenedPageNumber;
    }

    public int getLastOpenedPartNumber() {
        return lastOpenedPartNumber;
    }

    public void setLastOpenedPartNumber(int lastOpenedPartNumber) {
        this.lastOpenedPartNumber = lastOpenedPartNumber;
    }

    public int getAccessCount() {
        return accessCount;
    }

    public void setAccessCount(int accessCount) {
        this.accessCount = accessCount;
    }
}

Appreciate any support. Thanks.

itabdullah
  • 605
  • 1
  • 8
  • 22
  • adding a primary key requires data deletion since sqllite does not support adding primary keys so realm cannot do that. you need to drop the database(delete the realm) and create again https://stackoverflow.com/questions/33940233/realm-migration-needed-exception-in-android-while-retrieving-values-from-real – Karan Harsh Wardhan Mar 22 '19 at 07:14
  • what was your old primary key? please post old and new scheme. also if data not needed you can drop the db but this will **ERASE ALL DATA** and if not you can do a transformation from old db columns to new ones, either the new primary-key column and the new string column – Atef Hares Mar 22 '19 at 07:17
  • @Atef Hares, I had no primary key before. The AccessInfo schema is same as before except for making existing field bookId as primary key and changing existing field lastOpenedTimeStamp as String type. Also, I want to retain my old data in the realm db – itabdullah Mar 22 '19 at 07:56
  • Please use the schemaVersion properly as indicated by the Realm examples https://github.com/realm/realm-java/blob/7ca0c8c4f4715df9bacd586cb4bec66b78faf8b3/examples/migrationExample/src/main/java/io/realm/examples/ealmmigrationexample/model/Migration.java#L57-L90 – EpicPandaForce Mar 28 '19 at 13:52
  • I simply added schemaVersion(1) to configuration instance and it worked. Thanks to @EpicPandaForce – itabdullah Mar 29 '19 at 05:05

0 Answers0