-
|
I'm trying to wrap my head around CKSharing, and while I understand the reasoning for not supporting many-to-many as explained in the docs, I'm wondering why I could not simply make all components of a M2M relation "nested" under the root record? If we take the Reminder example, why could I not tie I feel like this being such a clear-cut use case, it doesn't make sense that it would not be supported at the CloudKit level even... 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Hi @MrSkwiggs, hierarchical sharing in iCloud is modeled via a single Many-to-many associations cannot be modeled as a single parent relationship. They have two parents. In your example, I'm not sure how associating a tag with a There are ways around this if you want to explore them, we just don't have any of it detailed in the docs because each has their pros and cons. The simplest thing you could do is simply not designate one of the relationships as a foreign key in the SQL schema: @Table struct ReminderTag {
let reminderID: Reminder.ID
let tagID: Tag.ID
}
/*
CREATE TABLE "reminderTags"(
"reminderID" TEXT NOT NULL REFERENCES "reminders"("id") ON DELETE CASCADE,
"tagID" TEXT NOT NULL
) STRICT
*/SQLiteData uses this schema description in order to figure out how to set up And second, if you really do want to go the route of associating tags with lists, then you could use an set of IDs to associate any number of tags with a reminder: @Table struct Tag {
let id: UUID
var title = ""
let remindersListID: RemidnersList.ID
}
@Table struct Reminder {
let id: UUID
var title = ""
var remindersListID: RemindersList.ID
@Column(as: Set<UUID>.JSONRepresentation.self)
var tagIDs: Set<Tag.ID> = []
}This will mean you have to do more work in SQL queries and in your app logic to deal with this set of IDs, but it's possible. |
Beta Was this translation helpful? Give feedback.
Hi @MrSkwiggs, hierarchical sharing in iCloud is modeled via a single
parentassociation. IfrecordBhas itsparentfield set torecordA, then whenrecordAis sharedrecordBwill be shared too. But this simplistic model breaks down if a record has two or more parents.Many-to-many associations cannot be modeled as a single parent relationship. They have two parents. In your example, I'm not sure how associating a tag with a
RemindersListhelps because the real problem is thatReminderTaghas two foreign keys:reminderIDandtagID. Further, is associating tags with lists the user experience you actually want? Won't people find it confusing that they can't use tags that were defined in one …