I am trying to run a lightweight migration in which I am changing the name of a model property from name to title. The database is already populated with few records. Those records must be preserved.
Here is my schema versions:
enum TripsSchemaV1: VersionedSchema {
static var versionIdentifier: String? = "Initial version"
static var models: [any PersistentModel.Type] {
[Trip.self]
}
@Model
class Trip {
var name: String
init(name: String) {
self.name = name
}
}
}
enum TripsSchemaV2: VersionedSchema {
static var versionIdentifier: String? = "name changed to title"
static var models: [any PersistentModel.Type] {
[Trip.self]
}
@Model
class Trip {
@Attribute(originalName: "name") var title: String
init(title: String) {
self.title = title
}
}
}
Migration plan:
enum TripsMigrationPlan: SchemaMigrationPlan {
static var schemas: [any VersionedSchema.Type] {
[TripsSchemaV1.self, TripsSchemaV2.self]
}
static var stages: [MigrationStage] {
[migrateV1toV2]
}
static let migrateV1toV2 = MigrationStage.lightweight(fromVersion: TripsSchemaV1.self, toVersion: TripsSchemaV2.self)
}
And finally the usage:
@main
struct TripsApp: App {
let container: ModelContainer
init() {
do {
container = try ModelContainer(for: [Trip.self], migrationPlan: TripsMigrationPlan.self, ModelConfiguration(for: [Trip.self]))
} catch {
fatalError("Could not initialize the container.")
}
}
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(container)
}
}
}
When I run the app, all my data for the Trips is gone and I get the following message on the output window.
Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown coordinator model version." UserInfo={NSLocalizedDescription=Cannot use staged migration with an unknown coordinator model version.}
Any ideas?