-
Notifications
You must be signed in to change notification settings - Fork 302
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
Default values for new properties #157
Comments
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:
|
Maybe an annotation for defining "defaults" for null values? E.g.
|
No response. Closing. -ut |
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
-ut |
With Usage: @DefaultValue("")
String exampleProperty; @DefaultValue("")
var exampleProperty: String = "" Edit: This annotation currently simply adds the new built-in NullToEmptyStringConverter to the property: objectbox-java/objectbox-java/src/main/java/io/objectbox/converter/NullToEmptyStringConverter.java Lines 8 to 22 in 220fc35
So to customize default values, create your own converter and replace @DefaultValue with @Convert .
|
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. |
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? |
@pratikbutani The default of booleans is false. Are you saying you experienced something else? |
I want default true, How can I do that? |
Still present in v2.9.1 - Default values for new non-string (booleans, integers...) fields are created as As far as I know, our only workaround options are to :
Having an annotation that allows to set these default values on the |
This comment was marked as duplicate.
This comment was marked as duplicate.
@greenrobot, would you have any update on this? I just ran into this issue and am not quite sure either how to handle it. Here, it's not even just about primitive types, but about As I am initilalise the new list member with a default value, my assumption was ObjectBox was handling this transparently. First running the class'es default initialisation and then applying to that instance any data it has stored. That does not seem to be the case. Would that actually be an idea? Respectively, how should this be handled right now? Thanks. |
While
Unfortunately that does not work either, as it expects a primitive wrapper, even though ObjectBox does support I am sure there may be technical reasons for that, but I am afraid this limitation really is in contrast to ObjectBox's approach of handling database changes transparently. Is there a reason why ObjectBox ignores the default value for new fields (which are not in the database) and specifically sets them to Java's non-initialised counterparts? If ObjectBox could keep the value of the default initialiser for such fields, the whole issue would be fixed. Any comments much appreciated, @greenrobot-team, @greenrobot. |
There is a value in the database for new properties: it's Thanks for sharing your edge case with A workaround is to use a type supported with |
This comment was marked as duplicate.
This comment was marked as duplicate.
Update: using However, using |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@greenrobot-team To pick up on the annotation, maybe this would be better?
Where MyValueProvider would implement an interface "ValueProvider" which basically has a single method like |
Im using ObjectBox in a pet android project written in Kotlin.. I have a data class encoded as such (example):
Database already existed on the device.. I added an additional property to the
Foo
class (not in the data constructor):defaulted, non-null object
bar
.. however when pulling existing records from the stored box, that valuebar
appears to benull
.. 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)?
The text was updated successfully, but these errors were encountered: