Swift Data Fatal error: Duplicate keys of type 'AnyHashable' were found in a Dictionary

Hi @The-Wolf

What I did was add an id attribute to each of the data model classes and there isn’t any more crashes as far as I have been able to test.

@Model
final class Character {
    var id = UUID()
    var name: String
    var production: Production
    var myCharacter: Bool

    init(name: String, production: Production, myCharacter: Bool = false) {
        self.name = name
        self.production = production
        self.myCharacter = myCharacter
    }
}
@Model
final class Production {
    var id = UUID()
    var name: String

    init(name: String) {
        self.name = name
    }
}

I’ve edited your content view so that you can see the data that has been saved and also added a swipe gesture so that you can delete line items to see the effect.

struct ContentView: View {
    @Environment(\.modelContext) private var context
    @State private var showingSheet = false
    @Query var characters: [Character]
    var body: some View {
        VStack {
            List {
                ForEach(characters) { character in
                    Text(character.production.name + " - " + character.name)
                        .swipeActions(edge: .trailing, allowsFullSwipe: true) {
                            Button("Delete") {
                                // Delete an item from the db
                                context.delete(character)
                            }
                            .tint(.red)
                        }
                }
            }

            Button("Add", systemImage: "plus") {
                showingSheet.toggle()
            }
        }
        .sheet(isPresented: $showingSheet) {
            AddProductionView()
        }
    }
}

Am I right in assuming that this is related to a theatre production in that you have a Production with a whole lot of Characters. I make that assumption based on the fact that in the AddProductionView you first add a title (which is the Production name) and then add Characters that appear in that Production. Is that a fair assumption?

So your data model is a one to one in that the Production name appears more than once in the Production model and each of the related character names appear in the Character model.

I hope that helps a bit.
Cheers