From ccb63d62c04336ee96e17b2eb5e0846f2fef4f73 Mon Sep 17 00:00:00 2001 From: "Andrey.Tarashevskiy" Date: Tue, 14 Sep 2021 01:53:57 +0300 Subject: [PATCH] Table.id is not in record set #1341 --- .../exposed/sql/statements/InsertStatement.kt | 4 +- .../entities/EntityBugsRegressionTest.kt | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityBugsRegressionTest.kt diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt index 5633635443..3478a1fbb9 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt @@ -41,7 +41,7 @@ open class InsertStatement(val table: Table, val isIgnore: Boolean = } } - val firstAutoIncColumn = autoIncColumns.firstOrNull() + val firstAutoIncColumn = autoIncColumns.firstOrNull { it.autoIncColumnType != null } ?: autoIncColumns.firstOrNull() if (firstAutoIncColumn != null || returnedColumns.isNotEmpty()) { while (rs?.next() == true) { val returnedValues = returnedColumns.associateTo(mutableMapOf()) { it.first to rs.getObject(it.second) } @@ -147,7 +147,7 @@ open class InsertStatement(val table: Table, val isIgnore: Boolean = override fun prepared(transaction: Transaction, sql: String): PreparedStatementApi = when { // https://github.com/pgjdbc/pgjdbc/issues/1168 // Column names always escaped/quoted in RETURNING clause - autoIncColumns.isNotEmpty() && currentDialect is PostgreSQLDialect -> + autoIncColumns.isNotEmpty() && currentDialect is PostgreSQLDialect -> transaction.connection.prepareStatement(sql, true) autoIncColumns.isNotEmpty() -> diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityBugsRegressionTest.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityBugsRegressionTest.kt new file mode 100644 index 0000000000..d9e348bd64 --- /dev/null +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityBugsRegressionTest.kt @@ -0,0 +1,59 @@ +@file: Suppress("MatchingDeclarationName", "Filename") +package org.jetbrains.exposed.sql.tests.shared.entities + +import org.jetbrains.exposed.dao.EntityClass +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IdTable +import org.jetbrains.exposed.sql.Column +import org.jetbrains.exposed.sql.tests.DatabaseTestsBase +import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.junit.Test + +class `Table id not in Record Test issue 1341` : DatabaseTestsBase() { + + object NamesTable : IdTable("names_table") { + val first = varchar("first", 50) + + val second = varchar("second", 50) + + override val id = integer("id").autoIncrement().entityId() + + override val primaryKey = PrimaryKey(id) + } + + object AccountsTable : IdTable("accounts_table") { + val name = reference("name", NamesTable) + override val id: Column> = integer("id").autoIncrement().entityId() + override val primaryKey = PrimaryKey(id) + } + + class Names(id: EntityID) : IntEntity(id) { + var first: String by NamesTable.first + var second: String by NamesTable.second + companion object : IntEntityClass(NamesTable) + } + + class Accounts(id: EntityID) : IntEntity(id) { + var name: Names by Names referencedOn AccountsTable.name + + companion object : EntityClass(AccountsTable) { + fun new(accountName: Pair): Accounts = new { + this.name = Names.new { + first = accountName.first + second = accountName.second + } + } + } + } + + @Test + fun testRegression() { + withTables(NamesTable, AccountsTable) { + val account = Accounts.new("first" to "second") + assertEquals("first", account.name.first) + assertEquals("second", account.name.second) + } + } +}