objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

Identifier is set to zero when entity is fetched from database #545

Closed JulienLHPro closed 10 months ago

JulienLHPro commented 10 months ago

Bug description Identifier is set to zero when entity is fetched from database

Basic info

To Reproduce No idea, but it does not happen on all entities

Expected behavior When fetched from database, entity identifier should not be zero

Code

@Entity()
class ItemEntry {
  @Id()
  int internalID = 0;

  String clientID;
}

store.box<ItemEntry>().put(itemEntry, mode: PutMode.put)

After insertion, itemEntry.internalID is 1

ItemEntry? itemEntry = store
        .box<ItemEntry>()
        .query(generated.ItemEntry_.clientID.equals(clientID))
        .build()
        .findFirst();

itemEntry is not null and has all correct values, except for itemEntry.internalID which is 0

Logs, stack traces Because of all relations I skipped here, I end up with this error: "Bad state: Can't store relation info for the target object with zero ID"

Additional context The main insertion is done in a Transaction, but the problem is the same without it.

JulienLHPro commented 10 months ago

Additional note: on Admin page, internalID correctly starts at 1

greenrobot-team commented 10 months ago

Thanks for reporting! I don't see anything obvious. Can you provide a working unit test or minimal example project that reproduces this?

JulienLHPro commented 10 months ago

I fixed my issue but one might find interesting the fact that you can't set a relationship in the constructor: this.macroCategoryEntry.target = although you would get no error apart from getting a zero internalID when fetching that entity later

greenrobot-team commented 9 months ago

Good to hear. However, from the top of my head there should be nothing preventing you from setting a ToOne target in the constructor?