SwiftData Lightweight Migraton failed with VersionedSchema

Setup

I am running a versionedSchema for my SwiftData model and attempting a migration. The new version contains a new attribute, with a type of a new custom enum defined in the @Model class, a default value, and a private(set). Migration was completed with a migrationPlan with nil values for willMigrate and didMigrate.

Example - Previous Version

@Model
class MyNumber {
var num: Int
init() {
// Init Code
}
}

Example - Newest Version

@Model
class MyNumber {
var num: Int
private(set) var rounding: RoundAmount = MyNumber.RoundAmount.thirtyMinute
init() {
// Init Code
}
enum RoundAmount {
case fiveMinute, tenMinute, thirtyMinute
}
}

Issue

Running this code, I get a swiftData error for “SwiftData/ModelCoders.swift:1585: nil value passed for a non-optional keyPath, /MyNumber.rounding”

I assume this means a failure of the swiftData lightweight migration? I have reverted the version, removed private(set) and re-tried the migration with no success.

Using the versionedSchema with migrationPlans, are lightweight migrations possible? Could this be an issue with the use of a custom enum? Other changes in my actual project migrated successfully so I’m lost on why I’m having this issue.

Answered by DTS Engineer in 834007022

Yeah, enum RoundAmount is not Codable and so SwiftData doesn't know how to encode / decode MyNumber.rounding. You can fix the issue by making your enum Codable, like below:

enum RoundAmount: Int, Codable {
case fiveMinute = 5, tenMinute = 10, thirtyMinute = 30
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Yeah, enum RoundAmount is not Codable and so SwiftData doesn't know how to encode / decode MyNumber.rounding. You can fix the issue by making your enum Codable, like below:

enum RoundAmount: Int, Codable {
case fiveMinute = 5, tenMinute = 10, thirtyMinute = 30
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData Lightweight Migraton failed with VersionedSchema
 
 
Q