You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Default value support in protocol / generated code / database #1140
Allow the programmer to define a default value for a field in the protocol. The default value should propagate to the SQL table and the generated dart class. These defaults are in most cases constant and does not give a dynamic value.
The default "default" value should be null if field is nullable, if not it is required to be set. In case of List and Map I think it makes sense to initialize them as empty List/Map in the field is not nullable.
We should at least support the following types for default values
boolean
int
double
String
DateTime
Uuid
Duration
DateTime, the default value has to be an ISO8001 string OR now() where it would map to the same function in postgresql. Uuid, the default value has to follow the uuid syntax such as the nil uuid 00000000-0000-0000-0000-000000000000.
For other types we should not support default values (such as custom objects). We should allow defaults for any database type we have implemented.
Keywords
Configuring the default value could be done using the following keywords:
defualt - Sets the default value for both the Model and the storage.
defaultModel. - Sets the default value for only the Model.
defaultPersist - Sets the default value for only the storage.
Reacted by Mark O'Sullivan, Luke Hutchison, Danny, Bin Emmanuel, develocode777, Andy, alexeenkoff, Chris Norman, daniel-vom, Damian Bast and 10 more
Reacted by develocode777, Chris Norman, Sameer Ankalagi, Bin Emmanuel, Edisson Reinozo, Philip Nell, FantaMagier, Yehor Vialov and Lenz Paul
I definitely need this. I have a method that creates a default instance of a large protocol class, setting fields to '', 0, or false. Every time I modify the protocol file, this class breaks, and I have to go edit it.
Curious how you classify default values as a "nice to have" feature instead of an essential functionality in a migration/orm package? Most if not all ORMs I have encountered have this feature to ensure data consistency and simplify api calls for new entitities.
@alguintu it is classified as nice to have for the 1.2 update. This feature is something we want to build but we have to make a cut somewhere to get the release out. I agree that this is an important feature.
Hopefully when default values are added, this migration issue can be avoided:
One or more columns are added to table "social_post" which cannot be added in a table migration. The complete table will be deleted and recreated.
Columns should always be able to be added, without deleting the database, but I assume the reason this is not possible right now is because default values are not supported. The error message should actually explain that, even when default value support is added.
Other issues that would be fixed if we had default values:
Adding a column drops the whole table #2000 -- new columns with non-nullable values could be added without dropping the whole table if a default value could be assigned to the column.
Activity
MarkOSullivan94 commentedon Sep 11, 2023
Agree with all of this and for me this is one feature I'll be very excited to see added in the 1.2 release
lukehutch commentedon Sep 27, 2023
I definitely need this. I have a method that creates a default instance of a large protocol class, setting fields to
'',0, orfalse. Every time I modify the protocol file, this class breaks, and I have to go edit it.alguintu commentedon Dec 24, 2023
Curious how you classify default values as a "nice to have" feature instead of an essential functionality in a migration/orm package? Most if not all ORMs I have encountered have this feature to ensure data consistency and simplify api calls for new entitities.
Isakdl commentedon Dec 27, 2023
@alguintu it is classified as nice to have for the 1.2 update. This feature is something we want to build but we have to make a cut somewhere to get the release out. I agree that this is an important feature.
lukehutch commentedon Feb 20, 2024
Hopefully when default values are added, this migration issue can be avoided:
Columns should always be able to be added, without deleting the database, but I assume the reason this is not possible right now is because default values are not supported. The error message should actually explain that, even when default value support is added.
lukehutch commentedon Mar 9, 2024
Other issues that would be fixed if we had default values:
serverOnlyfields that also have a default value.