SwiftData many-to-many relationship doesn't populate the inverse.

I am using SwiftData in my iOS app and I created 2 models:

@Model
class Track {
    // ...

    var artists: [Artist]

    // ...

    init(artists: [Artist] = []) {
        self.artists = artists
    }
}

@Model
class Artist {
    // ...

    @Relationship(inverse: \Track.artists) var tracks: [Track]

    // ...

    init() {
        self.tracks = []
    }
}

And I populate my container like this:

//
// MyApp.swift
//

import SwiftUI
import SwiftData

@main
struct NyxApp: App {
    // TODO: handle exception
    let container = try! ModelContainer(for: Track.self, Artist.self)
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(container)
    }
}

//
// Function to add debug data
//

{
    let artist = Artist(
        // ...
    )

    let track = Track(
        // ...
        artists: [artist],
        // ...
    )
                    
    context.insert(track)
    try! context.save()
}

The Track entity stores the Artist in Track.artists temporarily, but when I restart the app, the array is empty. The Artist.tracks array won't even get populated after inserting the track. I didn't find much useful information about many-to-many relationships that would explain my issue.

Any help is appreciated, thank you!

SwiftData many-to-many relationship doesn't populate the inverse.
 
 
Q