0

I'm dealing with an issue related to ObjectBox migrations. Whenever I make a name change to any property in the app, if I re-run the flutter pub run build_runner build command to re-build the generated files, I'll get an error like the following if I try to run the app:

ObjectBoxException (ObjectBoxException: failed to create store: 10501 Incoming index ID 1:4292904108897114889 does not match existing UID 6540362680558265679)

I know from the error message that this is an issue related to one of the indexId values in the objectbox-model.json file. Using github, I can see that running the build_runner command has caused everywhere that uses an indexId value to generate a new value. If I search the IDs listed in the error message, I can confirm that the existing ID in my objectbox-model.json was repleaced by the incoming ID.

I've already ensured that every entity and property in the app is labeled with the appropriate UID values. Because of this, I don't see the normal ID values in objectbox-model.json being replaced with the build_runner command. However, I cannot find any reference online to this incoming index exception. The ObjectBox Docs Toubleshooting page does mention this exception, however this refers to the Java version. There is an explanation on how to use UIDs, but it only covers @Entity and @Property, which I already use. I've also attempted to search online for anyone else who's had this issue, however I can only find questions about incoming entity IDs, not index IDs.

I cannot provide an exact copy of my code due to company security policies, however here is a minor example of what the code looks like:

Class definitions:

@Entity(uid: 1111111111111111111)
class ChildType {
  @Property(uid: 2222222222222222222)
  int id = 0;

  @Property(uid: 3333333333333333333)
  final parent = ToOne<ParentType>();

  @Property(uid: 4444444444444444444)
  @Unique(onConflict: ConflictStrategy.replace)
  String childName;

  @Index()
  @Property(uid: 5555555555555555555, type: PropertyType.date)
  DateTime childBirthday;
}

@Entity(uid: 6666666666666666666)
class ParentType {
  @Property(uid: 7777777777777777777)
  int id = 0;

  @Backlink("parent")
  final children = ToMany<ChildType>();

  @Property(uid: 8888888888888888888)
  @Unique(onConflict: ConflictStrategy.replace)
  String parentName;

  @Index()
  @Property(uid: 9999999999999999999, type: PropertyType.date)
  DateTime parentBirthday;
}

@Entity(uid: 0000000000000000000)
class UnrelatedType {
  @Property(uid: 0101010101010101010)
  int id = 0;

  @Property(uid: 9898989898989898989)
  int renamedVar;  <--- renamed, causes database migration, requires build_runner command
}

objectbox-model.json:

"entities": [
  {
    "id": "1:1111111111111111111",
    "lastPropertyId": "4:5555555555555555555",
    "name": "ChildType",
    "properties" : [
      {
        "id": "1:2222222222222222222",
        "name": "id",
        "type":6 ,
        "flags" 1 
      },
      {
        "id": "2:3333333333333333333",
        "name": "parentId",
        "type": 11,
        "flags" 520,
        "indexId": "1:1231231231231231231",  <--- changes after build_runner command
        "relationTarget": "ParentType"
      },
      {
        "id": "3:4444444444444444444",
        "name": "childName",
        "type": 9,
        "flags" 34848,
        "indexId": "2:2342342342342342342"  <--- changes after build_runner command
      },
      {
        "id": "4:5555555555555555555",
        "name": "childBirthday",
        "type": 10,
        "flags" 8,
        "indexId": "3:3453453453453453453"  <--- changes after build_runner command
      }
    ],
    "relations": []
  },
  {
    "id": "2:6666666666666666666",
    "lastPropertyId": "3:9999999999999999999",
    "name": "ParentType",
    "properties" : [
      {
        "id": "1:7777777777777777777",
        "name": "id",
        "type":6 ,
        "flags" 1 
      },
      {
        "id": "2:8888888888888888888",
        "name": "parentName",
        "type": 9,
        "flags" 34848,
        "indexId": "4:4564564564564564564"  <--- changes after build_runner command
      },
      {
        "id": "3:9999999999999999999",
        "name": "parentBirthday",
        "type": 10,
        "flags" 8,
        "indexId": "5:5675675675675675675"  <--- changes after build_runner command
      }
    ],
    "relations": []
  },
  {
    "id": "3:0000000000000000000",
    "lastPropertyId": "2:9898989898989898989",
    "name": "UnrelatedType ",
    "properties" : [
      {
        "id": "1:0101010101010101010",
        "name": "id",
        "type":6 ,
        "flags" 1 
      },
      {
        "id": "2:9898989898989898989",
        "name": "renamedVar",
        "type":6
      }
    ],
    "relations": []
  }
]
  • You should not choose UIDs yourself, see the instructions at https://docs.objectbox.io/advanced/data-model-updates#how-to-and-example which also apply for Flutter/Dart (instead of re-building re-run build_runner). To resolve the Index UID conflict, see https://docs.objectbox.io/advanced/meta-model-ids-and-uids#manual-conflict-resolution – Uwe - ObjectBox May 15 '23 at 12:30
  • @Uwe-ObjectBox I did not choose the UIDs myself. The UIDs used here are fake UIDs to obfuscate confidential code. I set the UIDs by letting the objectbox-model.json file generate them first, then copying them into the UID parameter of the Property tags. And looking at the page you've linked here, I'm not sure I understand how to properly handle index UID conflicts. The issue isn't that different developers are setting different index UIDs, the issue is that a single developer running build_runner can cause the index UIDs to be regenerated and cause conflicts. – Mark Weinhold May 17 '23 at 20:09
  • I've tried to reproduce this, but failed to do so. The code that handles this also looks OK. Could you submit an issue at https://github.com/objectbox/objectbox-dart/issues with an example that can be used to reproduce? – Uwe - ObjectBox May 22 '23 at 11:48

0 Answers0