SwiftData error 134020 - "The model configuration used to open the store is incompatible with the one that was used to create the store."

Hi all,

I have tried everything (that i'm aware of) but i cannot seem to get around this error.

My app is setup using only SwiftData and is basically a custom version of the apps shown in SwiftData docs. I have done the following to try to debug:

  • Deleted app from device
  • Clean and re-build
  • Delete derived data and rebuild
  • I have tried reverting changes made to the @Models i have define
  • Every combination of the above steps + rebuilding

Im at a loss. No clue how to resolve this.

Some weird observations. This error only occurs after the first context.insert call. The first is successful and data is properly persisted across app launches. All subsequent attempts to insert result in the following error:

Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store." UserInfo={NSAffectedObjectsErrorKey=<NSManagedObject: 0x2824004b0> (entity: Track; id: 0x2807a7de0 <x-coredata:///Track/tB9B77486-8F60-4F63-848D-D1C5CC67BA526>; data: {

    createdAt = "2023-07-02 23:45:45 +0000";

    name = Test;

})}

How are you creating your ModelContainer? Is there only one container in your app?

You said you reverted your model changes. Did you change any of those classes after saving that first item?

Any solutions to this? I'm facing the same error, but with CoreData

Put a print before the ModelContainer init. And see if ModelContainer gets initialized more then once.. If so prevent multiple inits. Should solve the issue.

This also helped me. Thanks. Ini my case I'd make a singleton that held the container and I tried creating it again in another part of the app..

Hello! I had the same issue working with SwiftData and I found that creating my own ModelContainer in App.swift instead of relying on the default one worked.

// App.swift
import SwiftData
import SwiftUI

@main
struct OweApp: App {
  private var container: ModelContainer {
    try! ModelContainer(
      for: Lender.self, Transaction.self,
      configurations: .init()
    )
  }
  
  var body: some Scene {
    WindowGroup {
      HomeView()
    }
    .modelContainer(self.container)
  }
}

And the view file that uses the modelContext environment variable:

import SwiftData
import SwiftUI

struct HomeView: View {
  @Environment(\.modelContext) var modelContext
  @Query var lenders: [Lender]
  @Query var transactions: [Transaction]
  
  var body: some View {
    NavigationStack {
      List {
        ForEach(self.lenders) { lender in
          HStack {
            Text(lender.name)
            Spacer()
            Text("$\(lender.balanceOwed)")
              .font(.caption)
              .bold()
          }
        }
        .onDelete(perform: self.deleteLender)
      }
      .navigationTitle("Owe")
      .toolbar {
        ToolbarItem(placement: .topBarTrailing) {
          Button(action: self.addLender) {
            Text("New Lender")
          }
        }
      }
    }
  }
  
  private func addLender() {
    let firstNames = ["Miguel", "Salvador", "Mayra"]
    let lastNames = ["Aceves", "Maravillo", "Ochoa"]
    let lenderName = "\(firstNames.randomElement()!) \(lastNames.randomElement()!)"
    let lender = Lender(name: lenderName)
    
    self.modelContext.insert(lender)
  }
  
  private func deleteLender(offsets: IndexSet) {
    for offset in offsets {
      let lender = self.lenders[offset]
      self.modelContext.delete(lender)
    }
  }
}

#Preview {
  HomeView()
}
SwiftData error 134020 - "The model configuration used to open the store is incompatible with the one that was used to create the store."
 
 
Q