SwiftData integration for coexistence with CoreData Error: Persistent truncated

When integrating SwiftData for an already existing app that uses CoreData as data management, I encounter errors.

When building the ModelContainer for the first time, the following error appears:

Error: Persistent History (184) has to be truncated due to the following entities being removed (all Entities except for the 2 where I defined a SwiftData Model)

class SwiftDataManager: ObservableObject {
    static let shared = SwiftDataManager()
    private let persistenceManager = PersistenceManager.shared
    private init(){}

    lazy var modelContainer: ModelContainer = {
        do {
            let storeUrl = persistenceManager.storeURL()
            let schema = Schema([
                HistoryIncident.self,
                HistoryEvent.self
            ])
            let modelConfig = ModelConfiguration(url: storeUrl)
            return try ModelContainer(for: schema, configurations: [modelConfig])
        } catch {
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()

}
@Model public class HistoryIncident {
    var missionNr: String?
    @Relationship(deleteRule: .cascade) var events: [HistoryEvent]?
    
    public init(){}
}

@Model class HistoryEvent {
    var decs: String?
    var timestamp: Date?

    
    init(){}
}

As soon as I call the following function.

    func addMockEventsToCurrentHistorie() {
        var descriptor = FetchDescriptor<HistoryIncident>()
        let key =  self.hKey ?? ""
        descriptor.predicate = #Predicate { mE in
            key == mE.key
        }
        
        let historyIncident = try? SwiftDataManager.shared.modelContext.fetch(descriptor).first
        guard var events = historyIncident?.events else {return}
        events.append(contentsOf: createEvents())
    }

I get the error:

CoreData: error: (1) I/O error for database at /var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite. SQLite error code:1, 'no such column: t0.Z1EVENTS'

/var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite. SQLite error code:1, 'no such column: t0.Z1EVENTS' with userInfo of { NSFilePath = "/var/mobile/Containers/Data/Application/55E9D59D-48C4-4D86-8D9F-8F9CA019042D/Library/ Private Documents/appDatabase.sqlite"; NSSQLiteErrorDomain = 1; }

The error seems to indicate that your SwiftData model schema doesn't cover all the entities in your Core Data model, which tells SwiftData, intentionally or not, that the uncovered entities should be removed from the new schema. SwiftData hence does that when loading the store, and truncates the history because it isn't valid anymore.

If you make sure that each of the entities in your Core Data model is mapped to a SwiftData model class, the error should go away.

Accessing the same store using SwiftData and Core Data is demonstrated in the coexistence version of the following sample:

The discussion in the following post may also help provide more context:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData integration for coexistence with CoreData Error: Persistent truncated
 
 
Q