How can I use sub-queries in Exposed? I've tried alot of different things with aliases, but I always...
s

Stephcraft

over 1 year ago
How can I use sub-queries in Exposed? I've tried alot of different things with aliases, but I always get an error. This is the sort of SQL query I am trying to write in Exposed DSL:
SELECT *
FROM Profile
ORDER BY (SELECT SUM(kills) FROM PlayerRecord WHERE PlayerRecord.player_uuid = Profile.uuid)
This is what I've got so far:
// the column to use to sort the results
val sortingColumn = PlayerRecordTable.kills.sum() // I've also trying aliasing this, but get an error as well.

// create sub-query (alias suposedly mandatory here for Exposed to work properly with subQueries)
val aggregatedPlayerRecords = PlayerRecordTable
    .select(sortingColumn)
    .where {
        PlayerRecordTable.gameplaySessionID eq GameplayRecordTable.id
    }
    .alias("aggregatedPlayerRecords")

// get the column in question for the subQuery, this is not mandatory in SQL if the result is 1 row, 1 column
val aggregatedSortingColumn = aggregatedPlayerRecords[sortingColumn] // <- crashes here: java.lang.IllegalStateException: Field not found in original table fields

// if I add .alias("aggregatedSortingColumn"), then it crashes later on
// where the generated query only contains alias 'tokens' not the actual expression of the subQuery in the alias ...
// error: Unknown column 'aggregatedPlayerRecords.sortingColumn' in 'order clause'

val results = GameplayRecordTable.selectAll().orderBy(sortingColumn to SortOrder.DESC)
I'm getting same issues as these, but no clear solution is ever provided: • https://stackoverflow.com/questions/78003559/orderby-subquery-field-in-kotlin-exposedhttps://stackoverflow.com/questions/72463300/query-with-2-joins-and-subquery-using-kotlin-exposedhttps://github.com/JetBrains/Exposed/issues/1294 Thank you 🙏
hey after updating to latest kotlin and ksp version in multiplatform project I am getting build time...
a

Antonis Radz

7 months ago
hey after updating to latest kotlin and ksp version in multiplatform project I am getting build time error:
* Exception is:
org.gradle.internal.execution.WorkValidationException: A problem was found with the configuration of task ':shared:kspCommonMainKotlinMetadata' (type 'KspAATask').
  - Gradle detected a problem with the following location: '..../shared/build/generated/ksp/metadata/commonMain/kotlin'.
    
    Reason: Task ':shared:kspDebugKotlinAndroid' uses this output of task ':shared:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
    Possible solutions:
      1. Declare task ':shared:kspCommonMainKotlinMetadata' as an input of ':shared:kspDebugKotlinAndroid'.
      2. Declare an explicit dependency on ':shared:kspCommonMainKotlinMetadata' from ':shared:kspDebugKotlinAndroid' using Task#dependsOn.
      3. Declare an explicit dependency on ':shared:kspCommonMainKotlinMetadata' from ':shared:kspDebugKotlinAndroid' using Task#mustRunAfter.
Any ideas how to fix it?
ksp = "2.1.0-1.0.29"
kotlin = "2.1.0"
These lines are causing it:
dependencies {
    add("kspCommonMainMetadata", libs.koin.ksp.compiler)
    add("kspAndroid", libs.koin.ksp.compiler)
    add("kspIosX64", libs.koin.ksp.compiler)
    add("kspIosArm64", libs.koin.ksp.compiler)
    add("kspIosSimulatorArm64", libs.koin.ksp.compiler)
}

project.tasks.withType(KotlinCompilationTask::class.java).configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}
Thanks in advance!