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
When defining a model's id field with id: UuidValue, default=random, the generated code will always set the id upon model creation.
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
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.
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:
Activity
marcelomendoncasoares commentedon Apr 10, 2025
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
idfield to have eitherdefault=ordefaultPersist=. Thedefaultbehavior would remain creating the model value as designed (and as the key is intended to work), but a developer could usedefaultPersist=randomto make the model always null before saving to the database.@SandPod, do you have a take on this one?
SandPod commentedon Apr 10, 2025
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
defaultand the model getting an ID would be such an implicit action.Another suggestion could be to only allow the
defaultPersistanddefaultModelkeywords. 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 addserialto an int id in the model.So the most accurate solution is probably to only allow
defaultPersistfor theintid field anddefaultPersistanddefaultModelfor theUUIDid field.Additional consideration
An additional step is required in the framework to fully allow this.
When building the
insertquery we always strip away theidcolumn:serverpod/packages/serverpod/lib/src/database/sql_query_builder.dart
Lines 327 to 329 in 1e7e47d
With a test documenting it here:
serverpod/packages/serverpod/test/database/database_query/insert_sql_query_test.dart
Lines 83 to 96 in 1e7e47d