Realm-js: Realm at path already opened with different schema version.

Created on 8 Nov 2017  Â·  28Comments  Â·  Source: realm/realm-js

Goals

migrate the databse

Expected Results

database migrate

Actual Results

Realm at path '/data/data/com.realmmigration/files/default.realm' already opened with different schema version.

Steps to Reproduce

clone the project start the app on master then close the app
switch to branch version2 and restart the app

Code Sample

repo

Version of Realm and Tooling

  • Realm JS SDK Version: 2.0.4
  • React Native: 0.49
  • Client OS & Version: android 6.0 (not tested on ios)
  • Which debugger for React Native: None
O-Customer T-Bug

Most helpful comment

i found a solution ( see last commit )
if schemaVersion differ i open database with constructor and migration is called
maybe it's a bug in Realm.open or there is something missing in documentation

All 28 comments

What if you call realm.close() before closing the app? It shouldn't make a difference but it could be nice to know.

I have tried and same thing

@kneth
this is the code called before opening the databse

   if (Realm.schemaVersion(realmOption.path) !== realmOption.schemaVersion) {
      let db = await Realm.open({
        path: realmOption.path
      });
      db.close();
    }

Realm.schemaVersion is a read-only property on a Realm instance so I don't fully understand the snippet. Can you explain what you want to achieve?

This code do a close on the database if database schemaversion change

My point is that you try to call a static method schemaVersion() on Realm. Or is Realm a variable (and the Realm is overwritten)?

Yes I call the static method from imported realm

That is a bit strange as it is a property: https://realm.io/docs/javascript/2.0.4/api/Realm.html#schemaVersion

What error message do you get, when you use a property as a method?

Sorry, of course you use that function. My best guess is that if you do a either a hot or live reload of your app, the Realm instance is not closed. Can you verify that the app's process is terminated?

yes, i tried to kill app, restart phone, restart react packager and close the databse if schemaVersion differ (i have updated the repo with the close database).

i found a solution ( see last commit )
if schemaVersion differ i open database with constructor and migration is called
maybe it's a bug in Realm.open or there is something missing in documentation

Good to hear. I agree that our documentation could be more explicit. Migration is complex, and I believe that a proper example could help many developers. I will relabel this issue so we know what we need to document better.

I'm seeing this as well and believe there is a bug with Realm.open. I have provided a migration function and updated a schema and version all correctly per the documentation and this error is being thrown.

I'm also not getting any dice with @icfr 's solution of invoking the constructor manually

@jeremyong Please post your configuration, migration function and how you open the Realm.

Yeah I faced with the same issue today 👎
Realm.open throws an error "already opened with different schema version"

This is my code

Realm.open({
    path: `/realms/${company}/goods.realm`,
    schema: schema,
    schemaVersion: schemaVersion,
    migration: migration
})
.then(success)
.catch(error)

It looks like a bug in Realm.open, because

let realm = new Realm({
    path: `/realms/${company}/goods.realm`,
    schema: schema,
    schemaVersion: schemaVersion,
    migration: migration
})

works fine.

I think I can reproduce it in a simple test.

@icfr I did test the application with the way you describe in Steps to reproduce and was able to get the error you are getting.
My steps to repro were these

start master branch
leave the app open
checkout version2 branch
reload the app
got the "Realm at path already opened"

If force close the app I get different error

start master branch
force close the app
checkout version2 branch
start the app
got the "`Error: Migration is required due to the following …- Property 'Person.email' has been made required.`"

This error is expected since in version2 branch there are not migrations provided but a console.log in migration.js file.

The conclusion is that if you leave the app open and just reload the js code from RN you don't really restart the application. Which means the js code is only reloaded and the native C++ module Realm is using is left in memory. This is behaviour is the same as requiring a new js file which tries to open the same realm with new schema. That's why you see the error "Realm at path already opened".

So it works as designed. You can continue working on your application. Close this issue if you think it is answered.

cheers,
blagoev

there is a mistake in commit all have schema version at 2 ...
set schema version at 3 on version2 branch and see it

before opening the issue i have tested lot of thing (kill app, restart phone) and the only thing that work is to open realm with constructor

@icfr The solution helps me a lot, Thanks!
It makes me know more about Realm.open();

I had a misunderstanding of the migration by reading the docs, it showed an example with realm.open, I thought it works asynchronously actually it worked with const realm = new Realm(...) synchronously.

Its even better.

I faced with the same issue.
My problem was created instance and never closed.

const realm = new Realm()

// adding this line fixed my problem
realm.close()

Hi
I am facing the same issue
basically, I want to integrate the Logout functionality in App.

On Logout action, I am trying to delete Realm DB
like

function logout() {
 realmInstance.close();
 console.log('is Realm db is closed', realmInstance.isClosed); >> true
 Realm.deleteFile(path, encryptionKey);
 realmInstance = null;
 return true;
}

Which sucessfully closed realm db instance and also delted default.Realm file from machine
After this Navigating the user on Login screen. When I do the login action again the app is throwing the error that error '/CoreSimulator/Devices/8D580DF1-CA0B-420B-818F-0DE10DA18EF0/data/Containers/Data/Application/641DC82F-8428-4DDC-9CB2-0F3F54D2164A/Documents/default.realm' already opened on current thread with different schema.'.

Now when I am running the same thing by keeping the debugger mode on it works fine for me.
but in normal mode, it's failing Please give me some guideline. Thanks in advance.

@Dhirajbhujbal maybe you can use different path at creation

@Dhirajbhujbal Are you certain that you have closed all instances? The error message indicates that you haven't.

In React Native every time I altered some data and make migration I also had this error when Debugger is active.

When I try to disable to Debugger mode in React Native. It solves the issue.

Hope it can help anyone using React Native.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MihaelIsaev picture MihaelIsaev  Â·  3Comments

laznrbfe picture laznrbfe  Â·  3Comments

gwanjar picture gwanjar  Â·  3Comments

kontinuity picture kontinuity  Â·  3Comments

CrystalRanita picture CrystalRanita  Â·  3Comments