I’ve started developing app with SwiftData and decided to move to VersionedSchema recently and it just fails when I try to add a new version of a schema with Code=134504 "Cannot use staged migration with an unknown coordinator model version."
When I initially added version and named it v1, all worked fine, but making any changes to future schemas bring the same error. (right now I’m on V2, but I just dropped data on my development devices).
import Foundation
import SwiftData
enum SchemaV2: VersionedSchema {
static var versionIdentifier: Schema.Version = Schema.Version(2, 0, 0)
static var models: [any PersistentModel.Type] {
[..., cardModel.self]
}
...other models...
@Model
final class cardModel {
var bank: bankModel
var name: String?
init(bank: bankModel, name: String? = nil) {
self.bank = bank
self.name = name
}
}
}
SchemaV3:
import Foundation
import SwiftData
enum SchemaV3: VersionedSchema {
static var versionIdentifier: Schema.Version = Schema.Version(3, 0, 0)
static var models: [any PersistentModel.Type] {
[..., cardModel.self]
}
...other models...
@Model
final class cardModel {
var bank: bankModel
var name: String?
var rewardType: RewardType?
init(bank: bankModel,
name: String? = nil,
rewardType: RewardType? = .cash
) {
self.bank = bank
self.name = name
self.rewardType = rewardType
}
}
}
MigrationPlan:
import SwiftData
enum MigrationPlan: SchemaMigrationPlan {
static var stages: [MigrationStage] {
[
MigrateV1toV2, MigrateV2toV3
]
}
static var schemas: [any VersionedSchema.Type] {
[SchemaV1.self, SchemaV2.self, SchemaV3.self
]
}
static let MigrateV1toV2 = MigrationStage.lightweight(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self
)
// I want to use a custom migration here, but even lightweight fails
static let MigrateV2toV3 = MigrationStage.lightweight(
fromVersion: SchemaV2.self,
toVersion: SchemaV3.self)
}
Not too sure how can I proceed, since I have versionIdentifier in every schema and they are semver. Clean builds, Mac reload, beta Xcode were tried as well with no luck. Searching for unknown coordinator model version
didn’t help much.
p.s.I initialise SchemaV1 like this:
enum SchemaV1: VersionedSchema {
static var versionIdentifier = Schema.Version(1, 0, 0) // Also tried to use such form with no luck: public static var versionIdentifier: Schema.Version {.init(1, 0, 0)}
static var models: [any PersistentModel.Type] {
[..., cardModel.self]
}
...models...
}
2
Answers
I encountered this exact problem when transitioning from an unversioned schema to a versioned one.
The issue for me was that I didn’t define all my models in the V1 schema, only the one that had changed. Defining all models resolved the issue.
The error essentially means "I can’t determine from which version to start migrating."
The migrator examines your current database and compares its schema with schema generated by each of your versioned schemas. If it doesn’t find a match, it fails with this error. If it does, it starts migrating from this version up.
This is what happened to me. The migrator looked at schema generated by my V1 version schema, it produced 1 table instead of 3 (I have 3 models), and concluded, "This is not the correct version to start migration" because the current database has three tables, not one.
So, make sure your database schema and schema generated by models in some of your version schemas are the same.
Tip: In your old version schemas, there is no need to copy and paste all the model code. You only need the code that participates in schema creation (stored attributes and relationships, basically), because those copies of the models are ony needed to calculate the old schemas to determine from which one to start migrating.
I was running into this issue.
I’m a long time CoreData user – relatively new to SwiftData.
My issue was this:
I was not including ALL MODELS in my latest Schema
models
.Example:
V1
V2
I had failed to include the previous schema’s models in the new schema’s models.
Once I did this, everything was happy.
Hopefully this helps someone running into this issue. 🙂