Realm-js: Updating primary key in migration

Created on 10 Jun 2017  路  6Comments  路  Source: realm/realm-js

Hello I'm new in having a problem in order to do migration
I want to update the primary key of an object and create new object in new created schema

here is my code of what I trying to do

error message : Cannot modify primary key after creation

const realm = new Realm({
  schema: [profileSchema, postsSchema, billSchema, billItemsSchema, postImages],
  schemaVersion: 1,
  migration: function(oldRealm, newRealm) {
    if (oldRealm.schemaVersion < 1) {
      let oldObjects = oldRealm.objects('posts'),
          newObjects = newRealm.objects('posts'),
          arr = [];
      for (let i = 0; i < oldObjects.length; i++) {
        let image1 = oldObjects[i].image1,
            image2 = oldObjects[i].image2,
            image3 = oldObjects[i].image3,
            image4 = oldObjects[i].image4;
        newObjects[i].id = oldObjects[i].key;
        arr.concat(image1, image2, image3, image4);
        realm.write(() => {
          let posts = realm.objects('posts')[i];
          arr.map((val, x) => {
            posts.images.push({id: oldObjects[i].key + x, image: val});
          })
        })
      }
    }
  }
});

Anybody can guide can help me to solve this

Thanks in advance

T-Bug

All 6 comments

Hi there is a bug and we are working on fixing it for this issue.

@blagoev ok...thanks for the response

well at least I know someone is fixing it

I had the same issue, I was trying to change a primary key from int to string and I had to write a migration like:

```
var oldObjects=oldRealm.objects("Client");
var newObjects=newRealm.objects("Client");
newObjects["clie_id"]=0;

```
The bad thing about this is that now I lost each id value within Client

@tgoyne Are you working on this or it is done and I can update the issue?

This was fixed by https://github.com/realm/realm-object-store/pull/471, which has been pulled in here.

Is just me with this problem at [email protected] ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashah888 picture ashah888  路  3Comments

kevinnguy picture kevinnguy  路  3Comments

bdebout picture bdebout  路  3Comments

texas697 picture texas697  路  3Comments

ugendrang picture ugendrang  路  3Comments