How to chain .including to eagerly load a three level hierarchy? #1590
-
Hey folks! I have the following models that are in a relationship of:
I want to load the entire Program eagerly into the following model: struct FullProgram: Decodable, FetchableRecord {
var program: Program
var routines: [FullRoutine]
}
struct FullRoutine: Decodable, FetchableRecord {
var routine: Routine
var sessions: [FullSession]
}
struct FullSession: Decodable, FetchableRecord {
var session: ProgrammedSession
var sets: [ProgrammedSet]
} These are my record types: Program: struct Program: Identifiable, Hashable, Equatable, Codable, FetchableRecord, MutablePersistableRecord {
var id: String
[...]
static let routines = hasMany(Routine.self)
var routines: QueryInterfaceRequest<Routine> { request(for: Program.routines) }
Routine: struct Routine: Identifiable, Equatable, Codable, FetchableRecord, MutablePersistableRecord {
var id: String
var programId: String
[...]
static let program = belongsTo(Program.self)
var program: QueryInterfaceRequest<Program> { request(for: Routine.program) }
static let sessions = hasMany(ProgrammedSession.self)
var sessions: QueryInterfaceRequest<ProgrammedSession> { request(for: Routine.sessions) }
ProgrammedSession: struct ProgrammedSession: Identifiable, Equatable, Codable, FetchableRecord, MutablePersistableRecord {
var id: String
[...]
var routineId: String
static let routine = belongsTo(Routine.self)
var routine: QueryInterfaceRequest<Routine> { request(for: ProgrammedSession.routine) }
static let sets = hasMany(ProgrammedSet.self)
var sets: QueryInterfaceRequest<ProgrammedSet> { request(for: ProgrammedSession.sets) }
ProgrammedSet: struct ProgrammedSet: Identifiable, Equatable, Codable, FetchableRecord, MutablePersistableRecord{
var id: String
var programmedSessionId: String
[...]
static let session = belongsTo(ProgrammedSession.self)
var session: QueryInterfaceRequest<ProgrammedSession> { request(for: ProgrammedSet.session) }
What i have is:
But i get the following error:
It works well if I only load one level, like this:
I am doing this so I can create an editable user copy of a read-only template program, so I want to create a copy of the entire hierarchy with new IDs and some minor modifications, like changing some enum values. I know I can also just load them in multiple steps, but I would like to do it one go for the sake of simplicity. Loading them in multiple steps is my Plan B if I can't sort this issue out. Can anyone help? I sadly couldn't find a related thread through the search! Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved the problem from a different angle by going for a request model of struct FullProgrammedSet: Decodable, FetchableRecord {
var programmedSet: ProgrammedSet
var programmedSession: ProgrammedSession
var programmedProgression: ProgrammedProgression
var routine: Routine
} and this query: try! appDatabase.reader.read { db in
try! ProgrammedSet
.including(required: ProgrammedSet.session
.including(required: ProgrammedSession.routine)
.including(required: ProgrammedSession.programmedProgression)
)
.asRequest(of: FullProgrammedSet.self)
.fetchOne(db)
} |
Beta Was this translation helpful? Give feedback.
Solved the problem from a different angle by going for a request model of
and this query: