Selecting only one table in a join query #371
-
|
Hi all, I'm building a music player in which I use import Foundation
import SQLiteData
enum Models
{
@Table
nonisolated struct Track: Identifiable
{
let id: UUID
let artistName: String
let title: String
let appleMusicId: String
}
@Table
nonisolated struct Playlist: Identifiable
{
let id: UUID
let name: String
let appleMusicId: String?
}
@Table
nonisolated struct PlaylistTrack: Identifiable
{
let id: UUID
let playlistId: Models.Playlist.ID
let trackId: Models.Track.ID
}
}(note that I needed to put this schema into an And I inserted some data into the database using this Schema. Now I'd like to get the @Observable
final class PlaylistModel
{
let playlistId: Models.Playlist.ID
@ObservationIgnored
@FetchOne
var playlist: Models.Playlist?
@ObservationIgnored
@FetchAll
var tracks: [Models.Track]
var name: String {
return playlist?.name ?? "Unknown Playlist"
}
init(playlistId: Models.Playlist.ID)
{
self.playlistId = playlistId
self._playlist = FetchOne(Models.Playlist.where { $0.id.eq(playlistId)})
// How to select only Models.Track here??
let query = Models.Track
.join(Models.PlaylistTrack.all) { $0.id.eq($1.trackId) && $1.playlistId.eq(playlistId) }
.select { $0.0 }
self._tracks = FetchAll(
query
)
}
}Is there a way only to get the first table in the query back here? Or should I define a kind regards, Tim |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @thinkpractice, what you have is correct, it just seems like there is a compiler bug with doing .select { tracks, _ in tracks } |
Beta Was this translation helpful? Give feedback.
Hi @thinkpractice, what you have is correct, it just seems like there is a compiler bug with doing
{ $0.0 }. Probably related to parameter packs. But if you spell out the arguments it works: