Skip to content

ToOne.targetId is not set to 0 when removing target object with large ID #740

@nikitadol

Description

@nikitadol

Is there an existing issue?

Build info

  • objectbox version: 4.3.0
  • Flutter/Dart version: 3.29.3 / 3.7.2
  • Build OS: macOS 15.5
  • Deployment OS or device: Any

Expected behavior

ToOne.target will be valid if the model was deleted and then added again with the same Id

Actual behavior

If I delete the model that I added to ToOne.target and then add it again, the link refers to an unknown id, although it is the same

Code

Code
import 'package:flutter/widgets.dart'; import 'package:objectbox/objectbox.dart'; import 'objectbox.g.dart'; Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); final Store store = await openStore(); final modelABox = store.box<ModelA>(); final modelBBox = store.box<ModelB>(); modelABox.removeAll(); modelBBox.removeAll(); final modelAId = '321'; print(fastHash(modelAId)); // 3305779717846957101 print( modelBBox.put(ModelB()..modelA.target = ModelA(id: modelAId)), ); // auto inc id print(modelABox.put(ModelA(id: modelAId))); // 3305779717846957101 print(modelBBox.getAll().first.modelA.targetId); // 3305779717846957101 print(modelBBox.getAll().first.modelA.target); // Instance of 'ModelA' print(modelABox.remove(fastHash(modelAId))); // true print(modelBBox.getAll().first.modelA.targetId); // 3305779717846957101 print(modelBBox.getAll().first.modelA.target); // null print(modelABox.put(ModelA(id: modelAId))); // 3305779717846957101 print( modelBBox.getAll().first.modelA.targetId, ); // 3305779715767271424 <- ????????????? print(modelBBox.getAll().first.modelA.target); // null } /// FNV-1a 64bit hash algorithm optimized for Dart Strings int fastHash(String string) { var hash = 0xcbf29ce484222325; var i = 0; while (i < string.length) { final codeUnit = string.codeUnitAt(i++); hash ^= codeUnit >> 8; hash *= 0x100000001b3; hash ^= codeUnit & 0xFF; hash *= 0x100000001b3; } return hash; } @Entity() class ModelA { @Id(assignable: true) int dbId; @Index() String id; ModelA({required this.id}) : dbId = fastHash(id); } @Entity() class ModelB { @Id() int dbId; final modelA = ToOne<ModelA>(); ModelB({this.dbId = 0}); }

Logs, stack traces

Logs
flutter: 3305779717846957101 flutter: 25 flutter: 3305779717846957101 flutter: 3305779717846957101 flutter: Instance of 'ModelA' flutter: true flutter: 3305779715767271424 flutter: null flutter: 3305779717846957101 flutter: 3305779715767271424 flutter: null

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions