greenDAO icon indicating copy to clipboard operation
greenDAO copied to clipboard

Default Values for Entity Properties

Open greenrobot opened this issue 12 years ago • 44 comments

See https://groups.google.com/forum/#!topic/greendao/cbbUkP6DdHs for a discussion.

Using SQLite's DEFAULT values probably won't work because greenDAO bind's all values for inserting.

Probably, the values should be set in the constructor

greenrobot avatar Jan 30 '12 11:01 greenrobot

I have another suggestion for this topic, see discussion

matecode avatar Jul 15 '13 14:07 matecode

+1 on this feature.

In general, having a more flexible KEEP mechanism would a large majority of my issues. I think that KEEP within methods is one way to do it, but I would personally love to have a way to override generated methods, including constructors. Accidental overrides could be avoided with annotations, if that's a concern.

Example use cases:

  • Add initialization inside constructors
  • Create property representations are stored in a certain format in the DB, but accessed as Objects from code.

shroff avatar Mar 06 '15 02:03 shroff

Another +1 it would make some stuff easier indeed ...

ghost avatar May 07 '15 22:05 ghost

Another +1 (sad to hear this request have been added 3 years ago :-( )

glureau avatar Jun 17 '15 09:06 glureau

+1 for this, It is good to have default values. Why not sure about this?!!!!

saeedsq avatar Jul 25 '15 17:07 saeedsq

+1 so that all properties should have default values

vodkhang avatar Aug 11 '15 02:08 vodkhang

+1 I also want this feature.

jongha avatar Aug 23 '15 10:08 jongha

+1 this is a must.

hemdatd avatar Aug 30 '15 07:08 hemdatd

+1. Yes, this is required as Double, Integer, Float, Boolean and other Wrapper datatypes are null when retrieved. It should be 0 or false in case of Boolean. This is highly required feature. GreenDao is a very good library and I don't wanna stop using it for such small features missing.

Hardik4560 avatar Aug 31 '15 08:08 Hardik4560

+1. Also primitive type like int instead of Integer will be welcome.

adamkotkowski avatar Sep 13 '15 19:09 adamkotkowski

+1 for this feature. I'd rather not set the defaults in the constructor with the generator overwriting code.

aishpant avatar Oct 15 '15 07:10 aishpant

+1 for this feature.

olegflo avatar Nov 10 '15 15:11 olegflo

+1 this one is really missing.

hkokocin avatar Nov 24 '15 07:11 hkokocin

+1 I also want this feature.

zeng1990java avatar Nov 30 '15 11:11 zeng1990java

+1 for this feature!

caiolopes avatar Dec 10 '15 18:12 caiolopes

+1 a much needed feature

akki-123 avatar Dec 10 '15 20:12 akki-123

+1 for this feature

shivakumars avatar Mar 08 '16 12:03 shivakumars

I love greendao. please

hanhnd76 avatar Mar 12 '16 17:03 hanhnd76

+1 Please add

source-creator avatar Mar 20 '16 21:03 source-creator

+1 I also want this feature.

ButSoft avatar Apr 23 '16 12:04 ButSoft

+1 needed for alot of us please add it

iballan avatar Jul 15 '16 09:07 iballan

OK, let's collect some requirements!

Something I really want to avoid is to use defaults managed by the database. Why? Because it would require querying the database after inserts/updates, which would add complexity and slowness. Not an option.

What would be pretty easy to implement is setting fields in an entity's default constructor to default values. Would that be an option?

greenrobot avatar Jul 15 '16 09:07 greenrobot

having this in the constructor will not prevent putting NULL values. how exactly is this adding queries?

yishai-glide avatar Jul 15 '16 10:07 yishai-glide

If the dev really wants to override with null, then why not?

The problem with letting the DB do it, is that we only might assume we have the current state (a certain default value). For example, if a dev updated the default value, but forgot about schema migration, greenDAO would have the new default value, but the DB would be actually using the old one. So this is dangerous. To prevent this, it would be an option to refresh the entity from the DB after insert/update to ensure a consistent state.

greenrobot avatar Jul 15 '16 10:07 greenrobot

Btw, if the problem is that you have nulls because of fields are of non-primitive types (e.g. Long/Integer/Float/Double): this is a different topic! Please be aware that you also can use primitive types. In that case, "defaults" will be 0 and not null. If you use the generator project, you need to enable it using notNull() on your property.

greenrobot avatar Jul 15 '16 10:07 greenrobot

since the defaults managed by Database is Not an option and it will affect the performance. You may add it to the contractor as well.

what I do for now:

public SurfaceEntity() {
    // KEEP CONSTRUCTOR-DEFAULT - put your custom constructor code here
    created = new Date();
    myOtherDefaultValue = false;
        SurfaceIsTransparent = false;
    // KEEP CONSTRUCTOR-DEFAULT END
}

public SurfaceEntity(Long id) {
    // KEEP CONSTRUCTOR-ID - put your custom constructor code here
    this(); // call default to initialize with default values
    // KEEP CONSTRUCTOR-ID END
    this.id = id;
}

you may add function for the properties to this for us.

something like:

surfaceEntity.addBooleanProperty("SurfaceIsTransparent")
 .setDefaultBool(false);

This is just a suggestions if it is possible.

Thank you

iballan avatar Jul 15 '16 11:07 iballan

For greenDAO 3, it might look like this:

@Property(defaultValue="true")
boolean valid;

@Property(defaultValue="new Date()")
Date created;

So a default value would simply be a string pasted where needed (e.g. constructor, if that proves to be sufficient for most people).

greenrobot avatar Jul 15 '16 11:07 greenrobot

@greenrobot When is this feature expected to be available ?

iballan avatar Aug 25 '16 12:08 iballan

@greenrobot any updates ?

umesh0492 avatar Jan 03 '17 18:01 umesh0492

Hope it will be realised soon. Highly required thing.

HeyAlex avatar Mar 11 '17 16:03 HeyAlex

the feature updates?

SpiritMan avatar Apr 11 '17 06:04 SpiritMan

+1 for this, any news?

wrestlingcarbonapple avatar May 04 '17 11:05 wrestlingcarbonapple

+1 , come on

chentao7v avatar Jun 05 '17 09:06 chentao7v

+1

vostreltsov avatar Jun 24 '17 18:06 vostreltsov

+1

TheCrafter avatar Jul 03 '17 20:07 TheCrafter

+1 It's been 2years now..come on

Hardik4560 avatar Jul 28 '17 10:07 Hardik4560

Just to note: with greenDAO 3 it is easy to have a custom constructor (annotate the generated one with @Keep) where you could set your default values. You may also modify your getters and setters to set or return some default value if appropriate. -ut

greenrobot-team avatar Aug 01 '17 09:08 greenrobot-team

@greenrobot-team please provide me a clear example. thank you very much

BigGitWorld avatar Aug 14 '17 06:08 BigGitWorld

@greenrobot you meat get default in java code, actually the default value is not in databases file ?

liujigang avatar Aug 23 '17 06:08 liujigang

@greenrobot @greenrobot-team So how to solve the default value issue when it comes to migration?

Caused by: android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Books.IS_MOST_READ

your solution is not working and not clear, you don't even provide us with a working example!

KhalidElSayed avatar Dec 28 '17 00:12 KhalidElSayed

+1

sanekyy avatar Apr 13 '18 20:04 sanekyy

Hey is this gonna happen or what?

samuelowino avatar Dec 15 '20 11:12 samuelowino

+1

ZheGuangZeng avatar Jun 12 '21 09:06 ZheGuangZeng

+1

silexion avatar Jan 08 '22 17:01 silexion