Post

Replies

Boosts

Views

Activity

SwiftData Many to One Cascade Delete not working as I expect in Xcode15 Beta 7 & 8
This approach worked in Beta 5, but does not work in Betas 7 & 8. (I skipped Beta 6, so I don't know if it worked then). I'm not sure if it's a SwiftData bug or a mistake on my part since the API has changed since the WWDC video. (I opened Apple Feedback: FB13120831) I am trying to implement a cascade delete on a many to one relationship. See the two models below. When I deleted the Recipe Model object, I expect that any of the FoodMenus that have that recipe will also be deleted. But they are not. The Recipe is deleted and the FoodMenu is still there. When I put a deleteRule: .cascade on the FoodMenu.recipe, the cascade does work. The Menu and the Associated recipe are both deleted. But that's not the behavior I want. @Model final class Recipe { var name: String var id: UUID @Relationship(deleteRule: .cascade, inverse: \FoodMenu.recipe) var menus: [FoodMenu]? init(name: String, id: UUID, menus: [FoodMenu]? = []) { self.name = name self.id = id self.menus = menus } } @Model final class FoodMenu { var name: String var id: UUID var recipe: Recipe? = nil //I believe the below definition should be the same behavior as above and it works as expected. relationship is nullified when the FoodMenu is deleted. Recipe left in place. // @Relationship (deleteRule: .nullify, inverse: \Recipe.menus) // var recipe: Recipe? = nil init(name: String, id: UUID) { self.name = name self.id = id } }
1
0
1.3k
Sep ’23