Skip to content

Commit

Permalink
Table.id is not in record set JetBrains#1341
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac authored and SchweinchenFuntik committed Oct 23, 2021
1 parent fda0309 commit ccb63d6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class InsertStatement<Key : Any>(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) }
Expand Down Expand Up @@ -147,7 +147,7 @@ open class InsertStatement<Key : Any>(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() ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Int>("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<Int>("accounts_table") {
val name = reference("name", NamesTable)
override val id: Column<EntityID<Int>> = integer("id").autoIncrement().entityId()
override val primaryKey = PrimaryKey(id)
}

class Names(id: EntityID<Int>) : IntEntity(id) {
var first: String by NamesTable.first
var second: String by NamesTable.second
companion object : IntEntityClass<Names>(NamesTable)
}

class Accounts(id: EntityID<Int>) : IntEntity(id) {
var name: Names by Names referencedOn AccountsTable.name

companion object : EntityClass<Int, Accounts>(AccountsTable) {
fun new(accountName: Pair<String, String>): 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)
}
}
}

0 comments on commit ccb63d6

Please sign in to comment.