Skip to content

Use non-nullable id field when defaultModel is set. #3429

Closed

Description

@SandPod

Problem to Solve

When defaultModel is used to define the id column it will always be initialized regardless of if it is provided or not. Therefore, there is no need to have this field nullable in the model.

Proposal

Emit a warning or force a change during code generation or validation if defaultModel: true is used and the id field is nullable. This encourages users to make the id field non-nullable, even though it's currently technically allowed to be nullable.

Use Case

Accessing a non-nullable field through a nullable getter just creates unnecessary null checks in the code.

Alternatives

N/A

Additional context

This helps guide developers toward safer usage patterns.

How experienced are you with this library?

None

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    area: models
    enhancement
    on Apr 16, 2025
  2. marcelomendoncasoares commented on Apr 16, 2025

    @marcelomendoncasoares
    Collaborator

    I can take this on. Just don't know if we need the warning, as it does not exist today for the usage with defaultModel in regular fields. I think we need to allow non-nullable - which will probably be everyone's first choice -, but I would leave the developer free to declare it as nullable with no warning, just as for other fields. Because someone could use it intentionally to force a obj.id = null after creation in some use case.

  3. moved this from Next 🌟 to Now 🔥 in Serverpod Frameworkson Apr 16, 2025
  4. SandPod commented on Apr 16, 2025

    @SandPod
    ContributorAuthor

    True, maybe we just add a simple hint?

  5. moved this from Now 🔥 to Done 🚀 in Serverpod Frameworkson Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: modelsenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions