Use default value when adding entity attribute

By Ritchie Annand on 30 Nov 2016

When you specify a default value for an entity attribute, OutSystems uses that when you make a new record and do not/fail to specify the value for that attribute.

However, when you publish your changes, the database is filled with the "blank" value instead of the default value.

To illustrate, if you add a new column called Quantity to multiply an expense by, you give it a default value of 1 so that the value comes out the same. However, the new Quantity column in the database is filled with zeroes

It's not hard to get around - you just have to make update SQL that sets all the zeroes or blanks to the default value - but it seems like it shouldn't be necessary :)

-- Ritchie Annand

Justin James1 Dec 2016

I like the idea in priciple, but without storing a "dirty bit" on every column (super inefficient!) this won't work.


Imagine this scenario:

* Default value is "bob"

* Record created, defaults to "bob"

* User edits record, sets it to "jim"

* User edits record, sets it to "bob"

* Default value changed to "john"

* Record that had the value deliberately set to "bob" by a user now get sets to "john"

See the problem? You need a flag on EACH column indicating if it was changed from its original value by application code. Even then, let's say a user brings up  record, makes no change to the default value and clicks "save"... they approve of the default values... do you change that when the default changes, or leave it be?

So fundamentally, this change is impossible to implement at a system level in a manner that actually will not destroy application data.

J.Ja

Paulo Ferreira2 Dec 2016

Justin, I read the idea as something slightly simpler, which is to just fill the default value on the first deploy that adds the column.

That's actually the behavior for Local Storage Entities in P10, if I'm not mistaken.

Justin James2 Dec 2016

Paulo -

That's not what it says. It very clearly about existing records in the database already having blank/wrong default values if you do not set it correctly first. The existing behavior for server DB is to set the value when you add the column too, so that wouldn't be the change.

J.Ja

Paulo Ferreira2 Dec 2016

Hi Justin,

I still don't read it that way, but you're right about the existing behavior. For some reason I thought it was different.

Cheers,

Paulo

Ritchie Annand23 Feb (2 days ago)

I can definitely see the issue if someone alters the Default Value from one value to another. There is no way to tell whether they "meant it" or not. I wouldn't advise updating any data in that case. If that is the sort of thing likely to happen often, I would advise people to have a default called "Default" or just use null/blank and have a function that provides the actual default value when Default or blank is encountered.

A possible alternative might be to have columns have an Update Records With Default Value setting that's False by default. Not sure I'm happy 

In this case, I meant specifically for new attributes, and it looks as though they may have actually added the feature in OutSystems 10. I just added a new Boolean column with a Default Value of True, published it, and the column is actually filled with True!

So, whichever dev is responsible for that, thank you :)