I have done several successful migrations in the past that usually involve either adding an item to the @Model or renaming an item.
I recently added 2 items. I duplicated the last Schema, added the 2 items, bumped up the Schema version, did the migration (all of these steps having been done multiple times before with success), and now the app crashes with _explanation: (String?) = nil. All items have been given default values.
I've literally done nothing different than I've done before. I am completely confused. I added an item in the last Schema/Migration and all worked. But adding an item with next Schema/Migration and it fails.
But if I delete the app on the simulator (or device) and run it fresh with this SchemaV15, it does not crash.
My Schema:
enum SchemaV15: VersionedSchema {
static var versionIdentifier: Schema.Version {
return Schema.Version(15,0,0)
}
static var models: [any PersistentModel.Type] {
[
Note.self,
PBStatsDetail.self,
MyNotes.self
]
}
@Model
class PBStatsDetail {
var id: UUID
var timestamp: Date
var totalSU: Int64 = 0
init(
id: UUID,
timestamp: Date,
var totalSU: Int64 = 0
) {
self.id = id
self.timestamp = timestamp
self.totalSU = totalSU
my main struct app:
@main
struct Pickleball_DrillsApp: App {
typealias MyNotes = SchemaV15.MyNotes
typealias Note = SchemaV15.Note
typealias PBStatsDetail = SchemaV15.PBStatsDetail
let container: ModelContainer
init() {
do {
container = try ModelContainer(
for:
MyNotes.self,
Note.self,
PBStatsDetail.self,
migrationPlan: DBMigrationPlan.self
)
}
catch {
fatalError("Failed \(error)")
}
}//init
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(container)
.environmentObject(Favorites())
}
}
}
enum DBMigrationPlan: SchemaMigrationPlan {
static var schemas: [any VersionedSchema.Type] {
[
......
SchemaV14.self,
SchemaV15.self
]
}
static var stages: [MigrationStage] {
[
......
migrateV13toV14,
migrateV14toV15
]
}
static let migrateV14toV15 = MigrationStage.custom(
fromVersion: SchemaV14.self,
toVersion: SchemaV15.self,
willMigrate: nil,
didMigrate: nil
)