Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Unique with replace strategy that update instead of deleting and inserting a new one #468

Open
AdelKanso opened this issue Sep 6, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@AdelKanso
Copy link

Right now, @unique with conflict: ConflictStrategy.replace remove the old object and insert a new one, this behavior causes a null relation if there's data that has relation to the old object(the one before deleting), and after deleting and inserting a new object, the related item's relations aren't updated to the new object.

@AdelKanso AdelKanso added the enhancement New feature or request label Sep 6, 2022
@Chris1234567899
Copy link

Chris1234567899 commented Sep 6, 2022

Agreed. This messes up relations big time and since neither non-integer primary keys nor composite indices are supported, relations are currently a nightmare to deal with. Unfortunately other packages like isar suffer pretty much the same issues :(

I'll try an ugly workaround in the meanwhile lol:



@Entity()
class TestModel {
  @Id()
  int objectId = 0;

  @Unique(onConflict: ConflictStrategy.replace)
  String id; // UUID

  String name;

  TestModel({
    required this.id,
    required this.name,
  });

  Future save() async {
    final existing = objectBox.store
        .box<TestModel>()
        .query(TestModel_.id.equals(id))
        .build()
        .findFirst();
    if (existing != null) {
      print('update');
      objectId = existing.objectId;
      objectBox.store.box<TestModel>().put(this, mode: PutMode.update);
    } else {
      print('insert');
      objectBox.store.box<TestModel>().put(this, mode: PutMode.insert);
    }
  }
}

//and you go like
var model = TestModel(id: Uuid().v4(), name: "Test 1");
model.save();

@AdelKanso
Copy link
Author

In case of saving multiple objects(putMany)? and what if there's a subobjects that I also want to handle?

@Vahid1919
Copy link

Thanks for the request! Please thumbs up the first post, if you are interested in this.

@greenrobot-team
Copy link
Member

greenrobot-team commented Oct 4, 2022

Thanks! At this point when using ConflictStrategy.replace with relations your code needs to take care of updating relations. Or maybe find a way to not having to use ConflictStrategy.replace in the first place.

AFAIK this is not different from traditional relational databases?

Anyhow, I could have sworn we documented this, but it's not. So I'll add a note to the docs to be careful when using relations.

@MahmoudArabyDev

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants