objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

DB's last entity ID 5 is higher than 4 from model

Open AndSync opened this issue 8 years ago • 12 comments

when I install a newer version of my app over an old release version caused this Exception ,how to solve it?

I once deleted the files in the folder objectbox-models and rebuild them when coding the new version. Is this the cause?

java.lang.RuntimeException: Unable to instantiate application com.taoche.qctt.app.App: io.objectbox.exception.DbException: DB's last entity ID 5 is higher than 4 from model
	at android.app.LoadedApk.makeApplication(LoadedApk.java:565)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4519)
	at android.app.ActivityThread.access$1600(ActivityThread.java:147)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:135)
	at android.app.ActivityThread.main(ActivityThread.java:5254)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
Caused by: io.objectbox.exception.DbException: DB's last entity ID 5 is higher than 4 from model
	at io.objectbox.BoxStore.nativeCreate(Native Method)
	at io.objectbox.BoxStore.<init>(BoxStore.java:197)
	at io.objectbox.BoxStoreBuilder.build(BoxStoreBuilder.java:308)
	at com.taoche.qctt.app.App.attachBaseContext(App.java:57)
	at android.app.Application.attach(Application.java:185)
	at android.app.Instrumentation.newApplication(Instrumentation.java:995)
	at android.app.Instrumentation.newApplication(Instrumentation.java:979)
	at android.app.LoadedApk.makeApplication(LoadedApk.java:560)
	... 10 more

old default.json

{
  "_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
  "_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
  "_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
  "entities": [
    {
      "id": "1:1952766103365863709",
      "lastPropertyId": "5:8534718537150799611",
      "name": "NewsReadEntity",
      "properties": [
        {
          "id": "1:4454213282712056488",
          "name": "id"
        },
        {
          "id": "4:4995671780322277960",
          "indexId": "3:1701554255184595914",
          "name": "saveTime"
        },
        {
          "id": "5:8534718537150799611",
          "name": "newsId"
        }
      ],
      "relations": []
    },
    {
      "id": "2:4681455186000581093",
      "lastPropertyId": "5:1878320121943278157",
      "name": "SearchHistoryEntity",
      "properties": [
        {
          "id": "1:2509681780674567262",
          "name": "id"
        },
        {
          "id": "3:4144773757208887841",
          "name": "type"
        },
        {
          "id": "4:1796059445680556055",
          "name": "content"
        },
        {
          "id": "5:1878320121943278157",
          "indexId": "4:7238548514032321082",
          "name": "saveTime"
        }
      ],
      "relations": []
    },
    {
      "id": "4:3560015495734375645",
      "lastPropertyId": "10:61457877014357435",
      "name": "UserInfoEntity",
      "properties": [
        {
          "id": "1:602973440470861549",
          "name": "userId"
        },
        {
          "id": "2:3246723179484410592",
          "name": "userName"
        },
        {
          "id": "3:4293227510605898252",
          "name": "avatar"
        },
        {
          "id": "4:3515152886667629792",
          "name": "profile"
        },
        {
          "id": "5:5567729676601844469",
          "name": "grade"
        },
        {
          "id": "6:1228345978650353268",
          "name": "livePermission"
        },
        {
          "id": "7:3329156168540452629",
          "name": "isEditor"
        },
        {
          "id": "8:7163292701191435143",
          "name": "hasSign"
        },
        {
          "id": "10:61457877014357435",
          "name": "id"
        }
      ],
      "relations": []
    },
    {
      "id": "5:3024685112334106772",
      "lastPropertyId": "3:8308424872576718774",
      "name": "VideoHistoryEntity",
      "properties": [
        {
          "id": "1:2617505269500745415",
          "name": "id"
        },
        {
          "id": "2:6785869119173123775",
          "indexId": "5:1880192976898178181",
          "name": "saveTime"
        },
        {
          "id": "3:8308424872576718774",
          "name": "newsId"
        }
      ],
      "relations": []
    }
  ],
  "lastEntityId": "5:3024685112334106772",
  "lastIndexId": "5:1880192976898178181",
  "lastRelationId": "0:0",
  "lastSequenceId": "0:0",
  "modelVersion": 4,
  "modelVersionParserMinimum": 4,
  "retiredEntityUids": [
    2176690214929686409
  ],
  "retiredIndexUids": [
    5712274539482648998,
    2649951790214073402
  ],
  "retiredPropertyUids": [
    4605692285024303790,
    397063222858956460,
    926282552069903016,
    4571064178529368696,
    8717211572541988533,
    1283673548410501397,
    8633068216894386619,
    1837391302522505772,
    8625297986036545488,
    8049703795930077205
  ],
  "retiredRelationUids": [],
  "version": 1
}

new default.json

{
  "_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
  "_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
  "_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
  "entities": [
    {
      "id": "1:7268819421552606349",
      "lastPropertyId": "3:6939051831393356699",
      "name": "NewsReadEntity",
      "properties": [
        {
          "id": "1:1265655690987026639",
          "name": "id"
        },
        {
          "id": "2:3748868742107361312",
          "indexId": "1:992960471105957472",
          "name": "saveTime"
        },
        {
          "id": "3:6939051831393356699",
          "name": "newsId"
        }
      ],
      "relations": []
    },
    {
      "id": "2:589186565352160929",
      "lastPropertyId": "4:2574504725553098708",
      "name": "SearchHistoryEntity",
      "properties": [
        {
          "id": "1:7394507230736567604",
          "name": "id"
        },
        {
          "id": "2:4011440010662506653",
          "indexId": "2:5189269226120590970",
          "name": "saveTime"
        },
        {
          "id": "3:3710097363655897871",
          "name": "type"
        },
        {
          "id": "4:2574504725553098708",
          "name": "content"
        }
      ],
      "relations": []
    },
    {
      "id": "3:3341669883490510788",
      "lastPropertyId": "13:5701479747961985769",
      "name": "UserInfoEntity",
      "properties": [
        {
          "id": "1:6109940089247558511",
          "name": "id"
        },
        {
          "id": "2:1619251787468389600",
          "name": "userId"
        },
        {
          "id": "3:3145953919205188275",
          "name": "userName"
        },
        {
          "id": "4:7487945478244672905",
          "name": "avatar"
        },
        {
          "id": "5:6775580645504193763",
          "name": "profile"
        },
        {
          "id": "6:7577744986006211462",
          "name": "grade"
        },
        {
          "id": "7:7104882922991678825",
          "name": "livePermission"
        },
        {
          "id": "8:8249101017412193529",
          "name": "isEditor"
        },
        {
          "id": "9:6005646416433093387",
          "name": "isBanned"
        },
        {
          "id": "10:2205641484297255349",
          "name": "bannedMsg"
        },
        {
          "id": "11:4630277244590003009",
          "name": "hasSign"
        },
        {
          "id": "12:5065561494695866755",
          "name": "sysMsgNum"
        },
        {
          "id": "13:5701479747961985769",
          "name": "nameLength"
        }
      ],
      "relations": []
    },
    {
      "id": "4:7190768811120423132",
      "lastPropertyId": "3:8438774495542963149",
      "name": "VideoHistoryEntity",
      "properties": [
        {
          "id": "1:4412742502218631092",
          "name": "id"
        },
        {
          "id": "2:6998696281007344674",
          "indexId": "3:4444069384638686879",
          "name": "saveTime"
        },
        {
          "id": "3:8438774495542963149",
          "name": "newsId"
        }
      ],
      "relations": []
    }
  ],
  "lastEntityId": "4:7190768811120423132",
  "lastIndexId": "3:4444069384638686879",
  "lastRelationId": "0:0",
  "lastSequenceId": "0:0",
  "modelVersion": 4,
  "modelVersionParserMinimum": 4,
  "retiredEntityUids": [],
  "retiredIndexUids": [],
  "retiredPropertyUids": [],
  "retiredRelationUids": [],
  "version": 1
}

AndSync avatar Feb 05 '18 09:02 AndSync

If you delete and re-generate the model file you have to delete the database (for example by uninstalling the app). Otherwise ObjectBox has no idea how to map existing data to the new model.

You can read more about how the ObjectBox model works. -ut

greenrobot-team avatar Feb 05 '18 13:02 greenrobot-team

Simplify this also, just like other stuff in objectbox, love objectbox..

jpvs0101 avatar Sep 07 '18 05:09 jpvs0101

Use old default.json

PengXianglin2017 avatar Sep 15 '18 02:09 PengXianglin2017

Use old default.json

Thanks so much. This works for me!

jahonn avatar May 07 '19 02:05 jahonn

I just had this problem after re-generating all default.json files, but after fresh-installing the app. Turned out, the allowBackup-attribute in the AndroidManifest.xml was set to true, so the incompatible db-file was restored during the fresh install. Interestingly, I only found this solution in a SO-thread for another problem (UID-mismatch): https://stackoverflow.com/a/55008507/433421

Turns out, this problem is even explained in the official docs, but it would help in the future, if you could list some of the most-occurring error messages here in the official docs in connection with this problem / solution for better discoverability:

https://docs.objectbox.io/advanced/meta-model-ids-and-uids#the-nuke-option

Or better yet, you could give the Nuke-option Pitfall its own linkable paragraph, and then link to it from here:

https://docs.objectbox.io/troubleshooting#dbexception-after-switching-git-branch-no-concurrent-data-model-modifications

(with a short explanation that this could also be a source for the same problem)

ajans avatar Mar 18 '22 16:03 ajans

Funny enough, I only had this problem in the Android 12 emulator and the Pixel 4 with Android 12, but not with the Samsung Galaxy S20 FE with Android 12, so there seem to be some behavioral differences between these devices regarding google app-backup.

ajans avatar Mar 18 '22 16:03 ajans

And yes, I could have restored the old default.json files, but during heavy development, it collects some dust in form of many migrations and retirements, which I don't want to have for the first release... :smile:

ajans avatar Mar 18 '22 16:03 ajans

@ajans Thanks! I updated the docs in that regard (updated Troubleshooting page, expanded conflict resolution docs).

greenrobot-team avatar Mar 21 '22 14:03 greenrobot-team

@greenrobot If I create a new entity, should the deafult.json file be updated? I notice nowadays it does not.

nickmuteti avatar Feb 12 '24 15:02 nickmuteti

@greenrobot If I create a new entity, should the deafult.json file be updated? I notice nowadays it does not.

Yes, when you build your project.

greenrobot avatar Feb 12 '24 15:02 greenrobot

What could be the issue on my IDE. When I built, no changes at all. I have created new entities, but none is included in the default.json.

The worst is when I upload the app to Play Store, Existing users get an error: DB's last entity ID 36 is higher than 28 from model

nickmuteti avatar Feb 12 '24 16:02 nickmuteti

Uh, that sounds like a serious problem with your process. It looks like you did not use git with your models file. Please have a look at the docs for your options: https://docs.objectbox.io/advanced/meta-model-ids-and-uids That's all we can do for you.

Edit: there is also a summary for this specific error at https://docs.objectbox.io/troubleshooting#dbschemaexception-dbs-last-id-is-higher

greenrobot avatar Feb 12 '24 16:02 greenrobot