Skip to content

ToOne.isNull returning false when target doesn't exist #732

Closed
@mihakrajnc

Description

@mihakrajnc

Issue Basics

  • ObjectBox version (are using the latest version?): 2.3.2
  • Reproducibility: always

Reproducing the bug

Description

When you have an entity that defines a ToOne relation to another entity, you can use isNull to check if the other entity is null. However, this does not work properly if the other entity does not exist in the database anymore.

For instance, you have an entity A that defines a ToOne relation to entity B. If you delete B, entity A will still retain the ID and so isNull will incorrectly return false. But when accessing entity B with target, the result will be null.

Entities

@Entity
data class A(
    @Id
    var dbId: Long = 0,
) {
    lateinit var content: ToOne<B>
}


@Entity
data class B(
    @Id
    var dbId: Long = 0,
)

Misc

Apologies if this is expected behaviour, but it seems counter intuitive to me. The workaround is simple, just check if target is null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions