Skip to content

Default values for new properties #157

Open

Description

@werelord

Im using ObjectBox in a pet android project written in Kotlin.. I have a data class encoded as such (example):

@Entity data class Foo(var name:String, var description: String) {
    constructor() : this("", "") // parameter-less constructor
    @Id var id: Long = 0L
    // other stuff
}

Database already existed on the device.. I added an additional property to the Foo class (not in the data constructor):

@Entity data class Foo(var name:String, var description: String) {
    constructor() : this("", "") // parameter-less constructor
    @Id var id: Long = 0L
    var bar: String = ""
    // other stuff
}

defaulted, non-null object bar.. however when pulling existing records from the stored box, that value bar appears to be null.. I would expect it to be a blank string (default for the parameter); is the violation of not-null guarantee coming from the Java interop?

I fixed the issue by nuking the database; afterwords the entity created and stored is not-null.. But this brings up any possible future updates that may violate this.

Is there a "database upgrade" method for updating already stored objects between versions, adding default values to new properties, or am I resigned to make anything added to an existing model be nullable (and handle it as such)?

Activity

  1. greenrobot commented on Aug 23, 2017

    @greenrobot
    Member

    It's quite easy to trick the Kotlin non-nullable types, e.g. with JSON parsers. Same is true for ObjectBox. We will target those kind of oddities in a future version. For now, we suggest to either use nullable types, or have some update code in place. Recipe for the latter:

    1. Have a flag (via SharedPrefs for example) like "fooBarChecked"
    2. If flag is false, run a migration and set the flag
    3. Migration: run a query to get all Foos with bar == null and update the resulting list
  2. greenrobot commented on Aug 25, 2017

    @greenrobot
    Member

    Maybe an annotation for defining "defaults" for null values? E.g.

    @DefaultForNull("")
    var bar: String = ""
    
  3. greenrobot-team commented on Apr 17, 2018

    @greenrobot-team
    Member

    No response. Closing. -ut

  4. greenrobot-team commented on Feb 26, 2019

    @greenrobot-team
    Member

    Reopening to discuss default values (in Java, but also Kotlin classes with default values).

    This also affects queries (merged from #589), e.g. if adding intProperty, then querying box with existing entities:

    box.query().equal(intProperty, 0) // no results
    box.query().isNull(intProperty) // has results
    

    -ut

  5. changed the title [-]Stored object "upgrade", defaulted property in Kotlin[/-] [+]Default values for new properties[/+] on Feb 26, 2019
  6. added this to the 2.6.0 milestone on Apr 28, 2020
  7. greenrobot-team commented on May 4, 2020

    @greenrobot-team
    Member

    With 2.6.0-RC there is very initial support for a @DefaultValue annotation. Currently only String properties are supported, and only the empty string "" as default value.

    Usage:

    @DefaultValue("")
    String exampleProperty;
    @DefaultValue("")
    var exampleProperty: String = ""

    Edit: This annotation currently simply adds the new built-in NullToEmptyStringConverter to the property:

    public class NullToEmptyStringConverter implements PropertyConverter<String, String> {
    @Override
    public String convertToDatabaseValue(String entityProperty) {
    return entityProperty;
    }
    @Override
    public String convertToEntityProperty(@Nullable String databaseValue) {
    if (databaseValue == null) {
    return "";
    }
    return databaseValue;
    }
    }

    So to customize default values, create your own converter and replace @DefaultValue with @Convert.

  8. greenrobot commented on May 4, 2020

    @greenrobot
    Member

    My first assessment is that we should keep default values separate from queries. E.g. if you want to, you can query for null values.

  9. modified the milestones: 2.6.0, 3.0 on Jun 15, 2020
  10. pratikbutani commented on Aug 5, 2020

    @pratikbutani

    What about the default values of booleans? I had taken a boolean variable. It's giving me the default value "false" but I want true then?

  11. greenrobot commented on Aug 5, 2020

    @greenrobot
    Member

    @pratikbutani The default of booleans is false. Are you saying you experienced something else?

  12. 11 remaining items

  13. neroux commented on Apr 18, 2023

    @neroux
  14. neroux commented on Apr 18, 2023

    @neroux
  15. neroux commented on Apr 24, 2023

    @neroux
  16. greenrobot commented on Apr 24, 2023

    @greenrobot
  17. neroux commented on Apr 24, 2023

    @neroux
  18. greenrobot commented on Apr 24, 2023

    @greenrobot
  19. neroux commented on Apr 24, 2023

    @neroux
  20. greenrobot commented on Apr 24, 2023

    @greenrobot
  21. neroux commented on Apr 24, 2023

    @neroux
  22. greenrobot commented on Apr 24, 2023

    @greenrobot
  23. neroux commented on Apr 24, 2023

    @neroux
  24. greenrobot commented on Apr 24, 2023

    @greenrobot
    Member

    @greenrobot-team To pick up on the annotation, maybe this would be better?

    @DefaultForNull(factory = MyValueProvider.class)

    Where MyValueProvider would implement an interface "ValueProvider" which basically has a single method like Object provideValue(). The advantage would be that it could provide complex types like lists, arrays and flex. Disadvantage: another thing passed down to JNI, and not clear if wrappers like Integer demand extra effort for primitive types like int.

  25. RobbWatershed commented on Sep 13, 2026

    @RobbWatershed
  26. greenrobot-team commented on Sep 14, 2026

    @greenrobot-team
  27. RobbWatershed commented on Sep 18, 2026

    @RobbWatershed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions