Added new field to swiftdata model, now crashes

I had a simple class called Entry with one field (timestamp: Date) and I tried to add a new field without having to uninstall the app , so I would not crash.

I tried implementing the MigrationPlan stuff but I do not know if I have to manually add a value to the new field or how exactly it works.

Here is my code. When I use the EntrySchemaV0 it works fine but if I try to use the V1 it crashes. (could not fetch ModelContainer) I also tried it with a custom migration stage. Crashes with error (SwiftData/BackingData.swift:432: Fatal error: Expected only Arrays for Relationships - String)

import Foundation
import SwiftData


enum EntrySchemaV0: VersionedSchema {
    static var versionIdentifier = Schema.Version(0, 1, 0)
    static var models: [any PersistentModel.Type] {
        [Entry.self]
    }
    
    @Model
    final class Entry {
        var timestamp: Date
        
        init(timestamp: Date) {
            self.timestamp = timestamp
        }
    }
}

enum EntrySchemaV1: VersionedSchema {
    static var versionIdentifier = Schema.Version(0, 2, 0)
    static var models: [any PersistentModel.Type] {
        [Entry.self]
    }
    
    @Model
    final class Entry {
        var name: String
        var timestamp: Date
        
        init(name: String = "", timestamp: Date = .now) {
            self.name = name
            self.timestamp = timestamp
        }
    }
}

enum EntryMigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] {
        [EntrySchemaV0.self, EntrySchemaV1.self]
    }
    static var stages: [MigrationStage] {
        [migrateV0toV1]
    }
    
    static let migrateV0toV1 = MigrationStage.lightweight(
        fromVersion: EntrySchemaV0.self,
        toVersion: EntrySchemaV1.self
    )
}

Reply to self:

I used custom and now it works

    static let migrateV1toV2 = MigrationStage.custom(
        fromVersion: SchemaV1.self,
        toVersion: SchemaV2.self,
        willMigrate: nil, didMigrate: nil
    )
    
    static let migrateV2toV3 = MigrationStage.custom(
        fromVersion: SchemaV2.self,
        toVersion: SchemaV3.self,
        willMigrate: nil, didMigrate: nil
    )

What I need tho, is a way to completly clear the stores and create a new model structure because I completely changed my models and know nothing is working

I'v seen the same error today. This seems to be a bug of SwiftData. I don't think SchemaMigrationPlan is ready for production.

SwiftData/BackingData.swift:432: Fatal error: Expected only Arrays for Relationships - String

Same issue today. Xcode 15.4 iOS 17.4 & 17.5. Submitted feedback FB13812722 (Complex Migrations with SwiftData do not work. Irrelevant Error: Fatal error: Expected only Arrays for Relationships - String)

Added new field to swiftdata model, now crashes
 
 
Q