I use SQL.swift but I cannot find a way to select values from multiple tables during a join. My db structure is the following:
airport --* tower --* tower_frequency
An airport can have several control towers and each control tower can have several radio frequencies. Given an airport ident
, I want to select all radio frequencies.
I build the query using
let airportTable = Table("airport")
let uid = Expression<Double>("id")
let ident = Expression<String>("ident")
let towerTable = Table("tower")
let frequencyTable = Table("tower_frequency")
var query = airportTable
.select(airportTable[*], towerTable[*], frequencyTable[*])
.join(towerTable, on: airportTable[uid] == towerTable[fkAirportId])
.join(frequencyTable, on: towerTable[uid] == frequencyTable[fkTowerId])
.filter(airportTable[ident] == icao)
The generated SQL is from query.asSQL()
is
SELECT "airport".*, "tower".*, "tower_frequency".* FROM "airport"
INNER JOIN "tower" ON ("airport"."id" = "tower"."fk_airport_id")
INNER JOIN "tower_frequency"
ON ("tower"."id" = "tower_frequency"."fk_tower_id")
WHERE ("airport"."ident" = 'SBP')
and it works fine when I run it manually in the db, yet SQL.swift
throws the exception No such table: "tower"