-
|
Hello there, I have the following struct and table, @Table
struct SequenceCount: Identifiable {
let id: UUID
@Column(as: [Int].JSONRepresentation.self)
let sourceSequence: SourceSequence
@Column(as: [Int].JSONRepresentation.self)
let targetSequence: TargetSequence
let count: Int
}which represents counts of 'transitions' from a source sequence (a short array of small integers) to a target sequence (another short array of small integers). (Actually, the If all I needed to do were insertions, then this for value in values {
let seqCountDraft = SequenceCount.Draft(
sourceSequence: value.sourceSequence,
targetSequence: value.targetSequence,
count: value.count
)
try SequenceCount.insert { seqCountDraft }
.execute(db)
}works, where But I also need to do updates, something like for value in values {
try SequenceCount
.update { $0.count += value.count }
.where {
$0.sourceSequence.eq(value.sourceSequence) &&
$0.targetSequence.eq(value.targetSequence)
}
.execute(db)
}but that doesn't compile. Also, the record to be updated isn't guaranteed to exist, in which case the update should become an insertion. So, I have two questions:
Many thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @wltrup, you should be able to do And upserts are only for a conflict of the primary key. Seems like you are wanting to update on |
Beta Was this translation helpful? Give feedback.
This won't work:
Hash values are not stable across launches of the app (nor are they guaranteed to be unique).
Something like this:
You could also just perform the update and check how many rows were changed, and if none were do an insert:
And there are some advanced tricks you…