我有一张桌子,上面有火车的当前位置,还有一张桌子,上面有车站的详细信息
object Train:Table("Train"){
val numb = integer("no")
val name= varchar("name", length = 40)
val cStnCode =varchar("cSC", length = 10).references(Stations.code)
val delay = integer("dly")
val srcCode = varchar("sC", length = 10).references(Stations.code)
val destCode = varchar("dC", length = 10).references(Stations.code)
val startDay = varchar("sD", length = 12)
override val primaryKey = PrimaryKey(numb, startDay)
}
object Stations:Table("Stations"){
val name= varchar("name", length = 50)
val code= varchar("code", length = 20)
override val primaryKey = PrimaryKey(code)
}
如何获取代码对应的站名。 我写的查询给出错误:有多个主键 - 外键引用。
db.dbQuery {
(Train innerJoin Stations)
.select {
(Train.cStnCode eq Stations.code)
(Train.srcCode eq Stations.code) and
(Train.destCode eq Stations.code)
}
.map {row->
TrainClass(
no = row[Train.numb],
na = row[Train.name],
cStC = row[Train.cStnCode],
cStN = row[Stations.name], // Station name from join
dly = row[Train.delay],
sC = row[Train.srcCode],
sN = "row[Stations.name]", // Station name from join
dC = row[Train.destCode],
dN = "row[Stations.name]", // Station name from join
}
}
如果我删除引用,那么我只会得到一个电台名称,即引用所在的位置。 我如何从车站表中获取所有三个车站名称
您可以使用别名,以便每个连接独立处理
stations
stations
中的每个引用定义
train
train
和每个别名 stations
对于结果中的每一行[我们可以将其放入
trainclass
的实例中]
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
object Train : Table("Train") {
val numb = integer("no")
val name = varchar("name", length = 40)
val cStnCode = varchar("cSC", length = 10).references(Stations.code)
val delay = integer("dly")
val srcCode = varchar("sC", length = 10).references(Stations.code)
val destCode = varchar("dC", length = 10).references(Stations.code)
val startDay = varchar("sD", length = 12)
override val primaryKey = PrimaryKey(numb, startDay)
}
object Stations : Table("Stations") {
val name = varchar("name", length = 50)
val code = varchar("code", length = 20)
override val primaryKey = PrimaryKey(code)
}
// Data for train details
data class TrainClass(
val no: Int,
val na: String,
val cStC: String,
val cStN: String,
val dly: Int,
val sC: String,
val sN: String,
val dC: String,
val dN: String
)
fun fetchTrainsWithStations() {
transaction {
// Alias for each foreign key relationship
val currentStation = Stations.alias("currentStation")
val sourceStation = Stations.alias("sourceStation")
val destinationStation = Stations.alias("destinationStation")
val query = Train.join(currentStation, JoinType.INNER, additionalConstraint = { Train.cStnCode eq currentStation[Stations.code] })
.join(sourceStation, JoinType.INNER, additionalConstraint = { Train.srcCode eq sourceStation[Stations.code] })
.join(destinationStation, JoinType.INNER, additionalConstraint = { Train.destCode eq destinationStation[Stations.code] })
.selectAll()
val result = query.map { row ->
TrainClass(
no = row[Train.numb],
na = row[Train.name],
cStC = row[Train.cStnCode],
cStN = row[currentStation[Stations.name]], // Current station name from join
dly = row[Train.delay],
sC = row[Train.srcCode],
sN = row[sourceStation[Stations.name]], // Source station name from join
dC = row[Train.destCode],
dN = row[destinationStation[Stations.name]] // Destination station name from join
)
}
println(result)
}
}