Updating an @Selection member #229
-
|
Using the column groups idea, I have a timestamps type @Selection
struct Timestamps {
var created: Date
var deleted: Date?
}I use this to simplify my tables @Table
struct Item {
...
var timestamps: Timestamps
}This works fine for fetching try Item
.find(id)
.update {
$0.timestamps.deleted = Date()
// ^ 🛑 Getter for 'subscript(dynamicMember:)' is unavailable
}At first I thought this meant you just can't assign through a property using try Item
.find(id)
.update {
$0.timestamps.deleted = #sql("\(Date())")
}I'll be honest, I don't get why this is the case. I went spelunking in the code but came up empty. For times when I do this a bunch I could add an extension like this: extension Updates<Item> {
mutating func delete(at date: Date) {
self.timestamps.deleted = #sql("\(date)")
}
}
// Now you can do this
try Item
.find(id)
.update {
$0.delete(at: Date())
}It's not a terrific solution though since you have to do this for each nested property you want to update. Is there a better approach to modifying |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
This is mainly due to a mismatch between // Use `#bind` macro to give the query language a hint.
$0.timestamps.deleted = #bind(Date())
// Make optionality explicit:
$0.timestamps.deleted = Optional(Date())We can explore making this nicer, though. Thanks for the example! |
Beta Was this translation helpful? Give feedback.
This is mainly due to a mismatch between
DateandOptional<Date>. Both of the following disambiguate things:We can explore making this nicer, though. Thanks for the example!