diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt index 05f5b7f431..19762859ae 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt @@ -43,7 +43,8 @@ class Column( fun nameInDatabaseCase(): String = name.inProperCase() - private val isLastColumnInPK: Boolean get() = table.primaryKey?.columns?.last() == this + private val isLastColumnInPK: Boolean + get() = this == table.primaryKey?.columns?.last() internal val isPrimaryConstraintWillBeDefined: Boolean get() = when { currentDialect is SQLiteDialect && columnType.isAutoInc -> false @@ -75,7 +76,7 @@ class Column( return listOf("ALTER TABLE ${tr.identity(table)} DROP COLUMN ${tr.identity(this)}") } - internal fun isOneColumnPK(): Boolean = table.primaryKey?.columns?.singleOrNull() == this + internal fun isOneColumnPK(): Boolean = this == table.primaryKey?.columns?.singleOrNull() /** Returns the SQL representation of this column. */ fun descriptionDdl(modify: Boolean = false): String = buildString { diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt index 6a969b085e..89397685ee 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt @@ -213,13 +213,12 @@ class EntityIDColumnType>(val idColumn: Column) : ColumnTyp override fun equals(other: Any?): Boolean { if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as EntityIDColumnType<*> - - if (idColumn != other.idColumn) return false - return true + return when (other) { + is EntityIDColumnType<*> -> idColumn == other.idColumn + is IColumnType -> idColumn.columnType == other + else -> false + } } override fun hashCode(): Int = 31 * super.hashCode() + idColumn.hashCode() diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 457bdaa058..ea243fbee8 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -1,5 +1,6 @@ package org.jetbrains.exposed.sql.tests.shared.ddl +import org.jetbrains.exposed.dao.id.IdTable import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.* @@ -36,6 +37,107 @@ class CreateTableTests : DatabaseTestsBase() { } } + @Test + fun testCreateIdTableWithPrimaryKeyByEntityID() { + val testTable = object : IdTable("test_table") { + val column1 = varchar("column_1", 30) + override val id = column1.entityId() + + override val primaryKey = PrimaryKey(id) + } + + withDb { + val singleColumnDescription = testTable.columns.single().descriptionDdl(false) + + assertTrue(singleColumnDescription.contains("PRIMARY KEY")) + assertEquals( + "CREATE TABLE " + addIfNotExistsIfSupported() + testTable.tableName.inProperCase() + " (" + + singleColumnDescription + + ")", + testTable.ddl + ) + } + } + + @Test + fun testCreateIdTableWithPrimaryKeyByColumn() { + val testTable = object : IdTable("test_table") { + val column1 = varchar("column_1", 30) + override val id = column1.entityId() + + override val primaryKey = PrimaryKey(column1) + } + + withDb { + val singleColumnDescription = testTable.columns.single().descriptionDdl(false) + + assertTrue(singleColumnDescription.contains("PRIMARY KEY")) + assertEquals( + "CREATE TABLE " + addIfNotExistsIfSupported() + testTable.tableName.inProperCase() + " (" + + singleColumnDescription + + ")", + testTable.ddl + ) + } + } + + @Test + fun testCreateIdTableWithNamedPrimaryKeyByColumn() { + val pkConstraintName = "PK_Constraint_name" + val testTable = object : IdTable("test_table") { + val column1 = varchar("column_1", 30) + override val id = column1.entityId() + + override val primaryKey = PrimaryKey(column1, name = pkConstraintName) + } + + withDb { + val singleColumn = testTable.columns.single() + + assertEquals( + "CREATE TABLE " + addIfNotExistsIfSupported() + testTable.tableName.inProperCase() + " (" + + "${singleColumn.descriptionDdl(false)}, " + + "CONSTRAINT $pkConstraintName PRIMARY KEY (${singleColumn.name.inProperCase()})" + + ")", + testTable.ddl + ) + } + } + + @Test + fun testCreateTableWithSingleColumnPrimaryKey() { + val stringPKTable = object : Table("string_pk_table") { + val column1 = varchar("column_1", 30) + + override val primaryKey = PrimaryKey(column1) + } + val intPKTable = object : Table("int_pk_table") { + val column1 = integer("column_1") + + override val primaryKey = PrimaryKey(column1) + } + + withDb { + val stringColumnDescription = stringPKTable.columns.single().descriptionDdl(false) + val intColumnDescription = intPKTable.columns.single().descriptionDdl(false) + + assertTrue(stringColumnDescription.contains("PRIMARY KEY")) + assertTrue(intColumnDescription.contains("PRIMARY KEY")) + assertEquals( + "CREATE TABLE " + addIfNotExistsIfSupported() + stringPKTable.tableName.inProperCase() + " (" + + stringColumnDescription + + ")", + stringPKTable.ddl + ) + assertEquals( + "CREATE TABLE " + addIfNotExistsIfSupported() + intPKTable.tableName.inProperCase() + " (" + + intColumnDescription + + ")", + intPKTable.ddl + ) + } + } + @Test fun primaryKeyCreateTableTest() { val account = object : Table("Account") {