Support optional Draft fields for non-optional Table columns #281
igorcamilo
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
Hi @igorcamilo, this was discussed in #30 and an attempt was made to implement it (#36), but there are still a lot of open questions. We would need to think through all of those subtleties before making changes. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
When using
@Tablewith non-optional fields that have SQLDEFAULTvalues (likecreatedAt/updatedAttimestamps), the generatedDrafttype mirrors the Swift default — not the SQL default. This means the SQLDEFAULTclause never fires on insert.Example
The generated
Draftinherits the Swift defaults:When creating
Entry.Draft(notes: "Hello"), the SQL becomes something like this:The
createdAtandupdatedAtcolumns get the draft creation time instead of SQLDEFAULT (now())(insertion time).Workarounds considered
Make fields optional in the Table itself (
var createdAt: Date?). This works —nilin Draft causes the column to be omitted from INSERT, so the SQL DEFAULT fires. But it forces optionals throughout the app, even though the value is never actuallynilafter a save.Use sentinel value and
AFTER INSERTtriggers to overwrite the sentinel. For example, one could useDate.distantPastorDate.distantFutureas a representation for unsaved entries. But this approach goes against Swift best practices.Desired behavior
A way to keep the field non-optional on the
Tabletype while making it optional onDraft, so that the SQLDEFAULTfires when omitted:This pattern is common for any field where the database owns the default value (timestamps, sequences, computed defaults) but the app always expects a non-nil value after reading.
I searched the existing issues and discussions in this repo and didn't find a prior request or solution for this. Happy to help with a PR if there's a direction you'd prefer.
Beta Was this translation helpful? Give feedback.
All reactions