Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPOSED-21 Primary key constraint not created #1728

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Column<T>(

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
Expand Down Expand Up @@ -75,7 +76,7 @@ class Column<T>(
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,12 @@ class EntityIDColumnType<T : Comparable<T>>(val idColumn: Column<T>) : 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -36,6 +37,107 @@ class CreateTableTests : DatabaseTestsBase() {
}
}

@Test
fun testCreateIdTableWithPrimaryKeyByEntityID() {
val testTable = object : IdTable<String>("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<String>("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<String>("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") {
Expand Down