I’m trying to restrict following model to “one per day”. It can reference 2 types of other structs (templet or preset) hence 2 different id’s. And it sets it’s creation day date to same one.
I was expecting #Unique constraint to work in a way where it “upserts” models with say same templateId and date, but it still creates new ones.
Am I doing something wrong, or misunderstanding how this is supposed to work?
import SwiftUI
import SwiftData
@Model
final class MySession {
#Index< MySession >(
[.presetId], [.templateId],
[.presetId, .createdAtDay],
[.templateId, .createdAtDay]
)
#Unique< MySession >([.presetId, .createdAtDay], [.templateId, .createdAtDay])
var presetId: UUID?
var templateId: UUID?
var createdAtDay: Date
init(
presetId: UUID?,
templateId: UUID?
) {
self.presetId = presetId
self.templateId = templateId
self.createdAtDay = Calendar.current.startOfDay(for: .now)
}
}