SwiftData inverse Relationship stop working in XCode 15 beta 7

This approach works before XCode 15 beta 7:

@Model
final class Item {
    var name: String
    @Relationship(inverse:\Note.item) var notes: [Note]
    init(name: String = "Item name") {
        self.name = name
        self.notes = []
    }
}
@Model final class Note {
    var name: String
    var item: Item
    init(name: String = "Note name", item: Item) {
        self.name = name
        self.item = item
    }
}
@main
struct LifeKPIs_SwiftData_PlaygroundApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: [Item.self, Note.self])
    }
}
struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    var body: some View {
       //...
       let item = Item()
       modelContext.insert(item)
       //...
       let note = Note( item: item) //Error here
    }
}

Thread 1: "Illegal attempt to establish a relationship 'item' between objects in different contexts (source = <NSManagedObject: 0x6000021c3020> (entity: Note; id: 0x60000030ef00 <x-coredata:///Note/t0C99AC11-423B-4942-9B5D-C9F9E6A4370E7>; data: {\n item = nil;\n name = "Note name";\n}) , destination = <NSManagedObject: 0x60000212d400> (entity: Item; id: 0xabdd96b5daee1528 <x-coredata://4C4C1274-3F87-425E-837F-F2AEFD751084/Item/p1>; data: {\n name = "New Item 0";\n notes = (\n );\n}))"

Making Item optional fixes the problem: var item: Item?

You might want to try something like this:

@Model
final class Note {
    var name: String
    var item: Item?
    init(name: String = "Note name") {
        self.name = name
    }

    // Move this out of init
    func setItem(_ item: Item) {
        self.item = item
    }
}

And call setItem after init. I had some luck with this when I tried to initialize another Model in a Model too.

SwiftData inverse Relationship stop working in XCode 15 beta 7
 
 
Q