Skip to content

Default value support in protocol / generated code / database #1140

Closed

Description

@Isakdl

Feature: default values.

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.

class: Example
table: example
fields:
  toggle: boolean, default=true
  score: int, default=0

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.

Activity

  1. added
    enhancement
    on Jul 12, 2023
  2. moved this to Version 1.2 - Essentials 🔥 in Serverpod Frameworkson Jul 12, 2023
  3. MarkOSullivan94 commented on Sep 11, 2023

    @MarkOSullivan94

    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

  4. lukehutch commented on Sep 27, 2023

    @lukehutch
    Contributor

    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.

  5. moved this from Version 1.2 - Essentials 🔥 to Version 1.2 - Nice to have 🥳 in Serverpod Frameworkson Oct 9, 2023
  6. alguintu commented on Dec 24, 2023

    @alguintu

    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.

  7. Isakdl commented on Dec 27, 2023

    @Isakdl
    CollaboratorAuthor

    @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.

  8. lukehutch commented on Feb 20, 2024

    @lukehutch
    Contributor

    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.

  9. lukehutch commented on Mar 9, 2024

    @lukehutch
    Contributor

    Other issues that would be fixed if we had default values:

  10. moved this from Later 🥳 to Now 🔥 in Serverpod Frameworkson Jun 13, 2024
  11. moved this from Now 🔥 to Done 🚀 in Serverpod Frameworkson Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions