diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt index d83ce3ea05..d1fe829413 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt @@ -301,7 +301,7 @@ open class Query(override var set: FieldSet, where: Op?) : AbstractQuer override fun count(): Long { return if (distinct || groupedByColumns.isNotEmpty() || limit != null) { fun Column<*>.makeAlias() = - alias(transaction.db.identifierManager.quoteIfNecessary("${table.tableName}_$name")) + alias(transaction.db.identifierManager.quoteIfNecessary("${table.tableNameWithoutSchemeSanitized}_$name")) val originalSet = set try { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt index 7eeddc61c3..0a4b0c2472 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt @@ -1,10 +1,10 @@ package org.jetbrains.exposed.sql.tests.shared.dml -import org.jetbrains.exposed.sql.alias -import org.jetbrains.exposed.sql.max -import org.jetbrains.exposed.sql.selectAll +import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.tests.DatabaseTestsBase +import org.jetbrains.exposed.sql.tests.currentDialectTest import org.jetbrains.exposed.sql.tests.shared.* +import org.jetbrains.exposed.sql.vendors.SQLServerDialect import org.junit.Test class CountTests : DatabaseTestsBase() { @@ -24,7 +24,7 @@ class CountTests : DatabaseTestsBase() { @Test fun `test that count() returns right value for Query with group by`() { - withCitiesAndUsers { _, user, userData -> + withCitiesAndUsers { _, _, userData -> val uniqueUsersInData = userData.select(userData.user_id).withDistinct().count() val sameQueryWithGrouping = userData.select(userData.value.max()).groupBy(userData.user_id).count() assertEquals(uniqueUsersInData, sameQueryWithGrouping) @@ -41,4 +41,29 @@ class CountTests : DatabaseTestsBase() { assertEquals(1L, OrgMemberships.selectAll().count()) } } + + @Test + fun testCountAliasWithTableSchema() { + val custom = prepareSchemaForTest("custom") + val tester = object : Table("custom.tester") { + val amount = integer("amount") + } + + withSchemas(custom) { + SchemaUtils.create(tester) + + repeat(3) { + tester.insert { + it[amount] = 99 + } + } + + // count alias is generated for any query with distinct/groupBy/limit & throws if schema name included + assertEquals(1, tester.select(tester.amount).withDistinct().count()) + + if (currentDialectTest is SQLServerDialect) { + SchemaUtils.drop(tester) + } + } + } }