swiftswift-dataswift-data-relationship

SwiftData: Type 'Schema.Relationship.Option' has no member 'cascade'


We have a Swiftdata model such as:

@Model
class MyThing {

   var name:String

   var myObject: MyCustomObject

   //...

}

In order for "myObject" (which is also an @Model) to automatically get deleted when an instance of MyThing is deleted, we need to add the attribute(?), Relationship(.cascade) to myObject.

This is shown in this tutorial:

enter image description here

... and this Apple video:

enter image description here

But when I try to do it, this happens (there seems to be no option to do it)... and if I manually type it out it says it's not found:

enter image description here


Solution

  • Those resources you found are out of date. There should be an argument label deleteRule:.

    @Relationship(deleteRule: .cascade)
    

    See also the signature of the macro in the documentation.

    macro Relationship(
        _ options: Schema.Relationship.Option...,
        deleteRule: Schema.Relationship.DeleteRule = .nullify,
        minimumModelCount: Int? = 0,
        maximumModelCount: Int? = 0,
        originalName: String? = nil,
        inverse: AnyKeyPath? = nil,
        hashModifier: String? = nil
    )
    

    Without an argument label, the compiler thought .cascade was passed to the options parameter.