SwiftData Migration Error: Refactoring existing models into an Inheritance hierarchy (iOS 26)

3 weeks ago 24
ARTICLE AD BOX

I am working on refactoring a SwiftData project originally built before iOS 26. I have three existing models—ModelA, ModelB, and ModelC—which share almost identical properties.

Back when I created the app, SwiftData didn't support inheritance, so I implemented them as three separate @Model entities. Now that inheritance is supported, I want to introduce a base class BaseModelD to clean up the architecture.

The Current Schema:

Swift

@Model class ModelA { var id: UUID var name: String var specificA: String } @Model class ModelB { var id: UUID var name: String var specificB: Int }

The Desired Schema:

Swift

@Model class BaseModelD { var id: UUID var name: String } @Model class ModelA: BaseModelD { var specificA: String } @Model class ModelB: BaseModelD { var specificB: Int }

The Problem: When I attempt to run a SchemaMigrationPlan, the migration fails. The error log indicates that SwiftData is unable to merge the existing tables ModelA and ModelB into the new unified table structure required for the inheritance hierarchy.

Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134110 "Bei der Migration des dauerhaften Speichers ist ein Fehler aufgetreten." UserInfo={sourceURL=file:///private/var/mobile/Containers/Shared/AppGroup/F212237B-6486-41ED-8A14-DAF3428D6EA2/Library/Application%20Support/MyApp.store, reason=Cannot migrate store in-place: Cannot merge multiple root entity source tables into one destination entity root table, destinationURL=file:///private/var/mobile/Containers/Shared/AppGroup/F212237B-6486-41ED-8A14-DAF3428D6EA2/Library/Application%20Support/MyApp.store, NSUnderlyingError=0x11a4dd1a0 {Error Domain=NSCocoaErrorDomain Code=134110 "Bei der Migration des dauerhaften Speichers ist ein Fehler aufgetreten." UserInfo={message=Cannot merge multiple root entity source tables into one destination entity root table, destinationRootEntity=BaseModelC, NSUnderlyingException=Cannot merge multiple root entity source tables into one destination entity root table, sourceRootEntities=( ModelA, ModelB ), reason=Cannot merge multiple root entity source tables into one destination entity root table}}}

It seems the migrator is struggling to map the existing persistent identifiers and move the data into the new "Single Table" layout that SwiftData uses for subclasses.

What I've tried:

import SwiftData enum AppMigrationPlan: SchemaMigrationPlan { static let schemas: [any VersionedSchema.Type] = [ SchemaV1.self, SchemaV2.self ] static let stages: [MigrationStage] = [ .custom( fromVersion: SchemaV1.self, toVersion: SchemaV2.self, willMigrate: { context in}, didMigrate: { context in }), ] }

Question: Is there a "clean" way or a known workaround in iOS 26 to migrate multiple existing independent tables into a single inheritance hierarchy without losing user data? Given that I have many more models than just these three, is there a scalable approach to this migration

Read Entire Article