SwiftData relationship crash on 17.x

Is this Relationship correct? Does this cause a circular reference? This runs on 18 but crashes on 17 in the swift data internals.

@Model
final class Item {
    @Attribute(.unique) var id: UUID
    var date: Date
    @Relationship(deleteRule: .nullify, inverse: \Summary.item) var summary: Summary?

    init(date: Date = Date.now) {
        self.id = UUID()
        self.date = Calendar.current.startOfDay(for: date)
        self.summary = Summary(self)
    }
}
@Model
final class Summary {
    @Attribute(.unique) var id = UUID()
    @Relationship var item: Item?

    init(_ item: Item) {
        self.item = item
    }
}

Answered by DTS Engineer in 809396022

Adding @Relationship before a relationship is fine, unless you specify the inverse parameter on both side, which will then trigger an error.

The error shown in your screenshot doesn't seem to be related to the existence of @Relationship in Summary. It is more likely related to the way you set up the object graph, which may be similar to the following post:

If the above post doesn't help, I'd be interested in taking a closer look into your code that reproduces the issue, if you don't mind to share that.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I only use the @Relationship macro at one end of the relationship to avoid any issues.

Maybe something has changed in the latest version that allows us to use it at both properties but in your case there is really no reason to do so.

I appreciate the reply. I saw it done both ways online and I left the @Relationship on Summary more as a reminder to me. But removing it gives me the same error.

I also removed the creation Summary from the Item initializer and created the Summary after init, but the same error remains so it's definitely something to do with the relationship.

And it still exists on 17.7.

Adding @Relationship before a relationship is fine, unless you specify the inverse parameter on both side, which will then trigger an error.

The error shown in your screenshot doesn't seem to be related to the existence of @Relationship in Summary. It is more likely related to the way you set up the object graph, which may be similar to the following post:

If the above post doesn't help, I'd be interested in taking a closer look into your code that reproduces the issue, if you don't mind to share that.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I was able to create a sample project this time showing the error. However, I'm unable to attach a zip file here, looks like only .json files are selectable. How can I send this to you?

I also found a post that had the same error, but with no resolution: @Relationship crash on ios17.5 but not on ios18

In the meantime, I will take a look through the post you referenced.

Thank you!

@DTS Engineer I have created Feedback, FB15481013, and attached a sample project showing the issue.

SwiftData relationship crash on 17.x
 
 
Q