A workaround for this is to specify a ModelConfiguration that uses a different database file for each type. My solution is based off the example code shown in wwdc2023-10196. It should be noted that the example code fails with the error "Cannot use instance member '' within property initializer; property initializers run before 'self' is available", so I had to move the ModelContainer initialisation into .modelContainer.
import SwiftUI
import SwiftData
@main
struct TestApp: App {
let fullSchema = Schema([
Item.self,
DuplicateItem.self
])
let itemModelConfiguration = ModelConfiguration(
schema: Schema([
Item.self
]),
url: URL.applicationSupportDirectory.appendingPathComponent("item.store")
)
let duplicateItemModelConfiguration = ModelConfiguration(
schema: Schema([
DuplicateItem.self
]),
url: URL.applicationSupportDirectory.appendingPathComponent("duplicateItem.store")
)
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(try! ModelContainer(for:
fullSchema,
itemModelConfiguration,
duplicateItemModelConfiguration)
)
}
}