In my iOS project, I use GRDB to manage a SQLite database. I try to query a table with a 1-1 association.
In my database, let's say I have 2 tables:
Selections
----------
- selectionId (primary key)
- position
- idSong (foreign key)
Songs
-----
- songId (primary key)
- name
- artist
Those two entities are linked with a 1-1 association, thanks to idSong
.
Here is what I tried:
- the
Selection
entity:
class Selection : Record
{
var selectionId: Int64?
var position: Int?
var idSong: Int64?
static let song = hasOne(Song.self, key: "selectionId", using: ForeignKey(["songId"]))
var song: QueryInterfaceRequest<Song> {
request(for: Selection.song)
}
// ...
}
- the
Song
entity:
class Song : Record
{
var songId: Int64?
var name: String?
var artist: String?
static let selection = belongsTo(Selection.self)
var selection: QueryInterfaceRequest<Selection> {
request(for: Song.selection)
}
// ...
}
- a
SelectionSong
struct:
struct SelectionSong : FetchableRecord
{
let selection: Selection
let song: Song
init(row: Row)
{
selection = row[Selection.databaseTableName]
song = row[Song.databaseTableName]
}
}
- here is how I created the two tables:
// Selections:
try db.create(table: Selection.databaseTableName) { table in
table.autoIncrementedPrimaryKey("selectionId")
table.column("position", .integer).notNull()
table.column("idSong", .integer)
.notNull()
.indexed()
.references(Song.databaseTableName, onDelete: .cascade)
}
// Songs:
try db.create(table: Song.databaseTableName) { table in
table.autoIncrementedPrimaryKey("songId")
table.column("name", .text).notNull()
table.column("artist", .text).notNull()
}
- and here is how I try to get a list of
SelectionSong
, so I can get a list ofSelection
, and for eachSelection
, the associatedSong
:
let request = Selection.including(optional: Selection.song)
let list = try SelectionSong.fetchAll(db, request)
but then I get the following error: missing scope 'song'
.
So how can I query my Selection
table, so I can get a list of SelectionSong
(a Selection
with the associated Song
)?
Thanks.