question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating primary key in migration

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tgoynecommented, Jul 19, 2017

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

1reaction
blagoevcommented, Jun 13, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change primary key in laravel migration - Stack Overflow
I'm trying to change the primary key on a table so I can use an other column as foreign key too. Here's the...
Read more >
ALTER PRIMARY KEY | CockroachDB Docs
To change an existing primary key without creating a secondary index from that primary key, use DROP CONSTRAINT ... PRIMARY KEY / ADD...
Read more >
Yet another Postgres migration: updating primary key type to ...
You have a table with a primary key constraint on a single integer column (let it be id ) and number of records...
Read more >
Changing an SQL Server primary key in Doctrine Migrations
I recently came across a rather weird quirk when trying to change a primary key in Sql Server using Doctrine Migrations: you need...
Read more >
defining primary keys in migration - Laracasts
Hi, I have implemented a table for letting twitter like following users. Here is the migration: Schema::create('follows', function (Blueprint $table) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found