Skip to content

Commit

Permalink
Fix creation of composite primary key in schema (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwheeler123 authored Jul 17, 2022
1 parent 7341e7d commit 2feb550
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {

// Primary keys

internal fun isCustomPKNameDefined(): Boolean = primaryKey?.let { it.name != "pk_$tableName" } == true
internal fun isCustomPKNameDefined(): Boolean = primaryKey?.let { it.name != "pk_$tableNameWithoutScheme" } == true

/**
* Represents a primary key composed by the specified [columns], and with the specified [name].
Expand All @@ -419,9 +419,9 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
/** Returns the columns that compose the primary key. */
val columns: Array<Column<*>>,
/** Returns the name of the primary key. */
val name: String = "pk_$tableName"
val name: String = "pk_$tableNameWithoutScheme"
) {
constructor(firstColumn: Column<*>, vararg columns: Column<*>, name: String = "pk_$tableName") :
constructor(firstColumn: Column<*>, vararg columns: Column<*>, name: String = "pk_$tableNameWithoutScheme") :
this(arrayOf(firstColumn, *columns), name)

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,4 +924,35 @@ class DDLTests : DatabaseTestsBase() {
}
}
}

@Test
fun createTableWithCompositePrimaryKeyAndSchema() {
val one = Schema("test")
val tableA = object : Table("test.table_a") {
val idA = integer("id_a")
val idB = integer("id_b")
override val primaryKey = PrimaryKey(idA, idB)
}

val tableB = object : Table("test.table_b") {
val idA = integer("id_a")
val idB = integer("id_b")
override val primaryKey = PrimaryKey(arrayOf(idA, idB))
}

withSchemas(excludeSettings = listOf(TestDB.SQLITE), schemas = arrayOf(one)) {
SchemaUtils.create(tableA, tableB)
tableA.insert { it[idA] = 1; it[idB] = 1 }
tableB.insert { it[idA] = 1; it[idB] = 1 }

assertEquals(1, tableA.selectAll().count())
assertEquals(1, tableB.selectAll().count())

if (currentDialectTest is SQLServerDialect) {
SchemaUtils.drop(tableA, tableB)
}

}
}

}

0 comments on commit 2feb550

Please sign in to comment.