Is it just me... during development there are many things that just go wrong (or sideways) (or backward). I find that deleting the CloudKit DB is way easier that creating migration scripts. And no user will be harmed - except maybe myself. So then I wonder where the Big Red button to blow up the SwiftData DB is....
I've found that this little bit of code will do the trick... put it in your App init() inside a do catch block AND leave it commented OUT until you need it. Then run the App with this code active. Close the App and comment out the code and the next run is in the green field of truth.
// // DELETE the DB and start Fresh
// container.deleteAllData()
// print("*** deleted the DB - starting Fresh ***")
init() {
let schema = Schema([Account.self])
let config = ModelConfiguration("Portfolio", schema: schema)
do {
print(URL.applicationSupportDirectory.path(percentEncoded: false))
container = try ModelContainer(for: schema, configurations: config)
// // DELETE the DB and start Fresh
// container.deleteAllData()
// print("*** deleted the DB - starting Fresh ***")
} catch {
fatalError("Could not configure the container \(error)")
}
// print(URL.applicationSupportDirectory.path(percentEncoded: false))
// print(URL.documentsDirectory.path())
}
}
Well I didn't receive the memo about this little tid bit... and when told that relationships (one-to-many, or many-to-many) require a property that points back to the other side. I just assumed - yeah it starts out nil - but I better shove a object in there...
Fatal error: Never access a full future backing data - PersistentIdentifier
Oh if you look close, there is verbage in the sample code commentary...
https://developer.apple.com/tutorials/develop-in-swift/work-with-relationships
The error message could be a bit more direct... as far as I know I didn't mess with it's future backing data...