Skip to content

UUID id fields get a value by default #3396

Closed

Description

@tp

Describe the bug

When defining a model's id field with id: UuidValue, default=random, the generated code will always set the id upon model creation.

Image

To reproduce

Create model with id: UuidValue, default=random

Expected behavior

The id field should stay optional, and retain a null value by default as long as it is not persisted in the database.

The developer can still choose to set an explicit id with the model's creation if they ensure that they'll call the correct insert or update DB method later.

Library version

main at 1c286f088a4901eb266b1678c1cfdae021ae20bb

Platform information

Additional context

No response

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Apr 8, 2025
  2. added
    area: models
    on Apr 9, 2025
  3. marcelomendoncasoares commented on Apr 10, 2025

    @marcelomendoncasoares
    Collaborator

    This behavior is actually by design, as discussed here - with the intention of allowing ids to be created on the client. But the current issue does have a point in case the developer prefer the model to always start null if not yet stored in the database.

    I think a middle term solution here could be allowing the id field to have either default= or defaultPersist=. The default behavior would remain creating the model value as designed (and as the key is intended to work), but a developer could use defaultPersist=random to make the model always null before saving to the database.

    @SandPod, do you have a take on this one?

  4. SandPod commented on Apr 10, 2025

    @SandPod
    Contributor

    Thank you for the reference in the discussion.

    I didn't pay enough attention there; I'm sorry for resolving it without revisiting this part.

    The previously expected behavior is that the id field is populated when something is inserted into the database (unless explicitly modified). For the ID field, I think we should preserve this assumption, meaning that generating IDs in the server or from the client will need to be an explicit action.

    I believe this is required, at least at the start, since adding default and the model getting an ID would be such an implicit action.

    Another suggestion could be to only allow the defaultPersist and defaultModel keywords. That would make generating ids a very explicit action. This will mostly only be usedful in combination with UUIDs since it will not be possible to add serial to an int id in the model.

    So the most accurate solution is probably to only allow defaultPersist for the int id field and defaultPersist and defaultModel for the UUID id field.

    Additional consideration

    An additional step is required in the framework to fully allow this.

    When building the insert query we always strip away the id column:

    var selectedColumns = _table.columns.where(
    (column) => column.columnName != 'id',
    );

    With a test documenting it here:

    group(
    'Given model with only id column when building insert query then default values are used in the query.',
    () {
    test(
    'when building insert query with a row then output is a valid SQL query that lists the columns.',
    () {
    var query = InsertQueryBuilder(
    table: Table<int>(tableName: 'only_id'),
    rows: [OnlyIdClass()],
    ).build();
    expect(query, 'INSERT INTO "only_id" DEFAULT VALUES RETURNING *');
    });
    });

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

Metadata

Metadata

Labels

area: modelsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions