Thanks to @Sweeper for pointing me to @Transient.
The following code works both my example, and real project:
import SwiftData
struct Foo {
var name: String
}
@Model
final class Bar {
var id: String
var name: String
@Transient
var foo: Foo = .init(name: "")
init(id: String, name: String) {
self.id = id
self.name = name
}
func changeFoo(newName: String) {
foo.name = newName
}
}