Skip to content

Commit

Permalink
[JetBrains/Exposed] SQLite autoincrement columns are not created (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Nov 3, 2019
1 parent 9960bd9 commit 93536d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
16 changes: 13 additions & 3 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Column.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.exposed.sql

import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
Expand Down Expand Up @@ -61,12 +62,21 @@ class Column<T>(val table: Table, val name: String, override val columnType: ICo
append(" ")
val isPKColumn = indexInPK != null
val colType = columnType
if (currentDialect is SQLiteDialect && colType.isAutoInc && table.columns.any{ it.indexInPK != null}) {
append(colType.sqlType().removeSuffix(" AUTOINCREMENT")) // Workaround as SQLite Doesn't support both PK and autoInc in DDL
val isSQLiteAutoIncColumn = currentDialect is SQLiteDialect && colType.isAutoInc

if (!isPKColumn && isSQLiteAutoIncColumn) {
tr.throwUnsupportedException("Auto-increment could be applied only to primary key column")// Workaround as SQLite Doesn't support both PK and autoInc in DDL
}

if (isSQLiteAutoIncColumn && !isOneColumnPK() && table.columns.any{ it.indexInPK != null}) {
append(currentDialect.dataTypeProvider.integerType()) // Workaround as SQLite Doesn't support both PK and autoInc in DDL
} else {
append(colType.sqlType())
}


// append(colType.sqlType())

val _dbDefaultValue = dbDefaultValue
if (!isPKColumn && _dbDefaultValue != null) {
val expressionSQL = currentDialect.dataTypeProvider.processForDefaultValue(_dbDefaultValue)
Expand All @@ -88,7 +98,7 @@ class Column<T>(val table: Table, val name: String, override val columnType: ICo
append(" NOT NULL")
}

if (isOneColumnPK()) {
if (isOneColumnPK() && !isSQLiteAutoIncColumn) {
append(" PRIMARY KEY")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager

internal object SQLiteDataTypeProvider : DataTypeProvider() {
override fun integerAutoincType(): String = "INTEGER AUTOINCREMENT"
override fun longAutoincType(): String = "INTEGER AUTOINCREMENT"
override fun integerAutoincType(): String = "INTEGER PRIMARY KEY AUTOINCREMENT"
override fun longAutoincType(): String = "INTEGER PRIMARY KEY AUTOINCREMENT"
override fun floatType(): String = "SINGLE"
override fun booleanToStatementString(bool: Boolean) = if (bool) "1" else "0"
override fun dateTimeType(): String = "NUMERIC"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,12 @@ class DDLTests : DatabaseTestsBase() {
val id = integer("id").autoIncrement()
val name = varchar("name", 42).primaryKey()
val age = integer("age").nullable()
// not applicable in H2 database
// val testCollate = varchar("testCollate", 2, "ascii_general_ci")
}

withTables(excludeSettings = listOf(TestDB.MYSQL, TestDB.ORACLE, TestDB.MARIADB), tables = *arrayOf(TestTable)) {
val shortAutoIncType = if (currentDialectTest is SQLiteDialect)
currentDialectTest.dataTypeProvider.integerAutoincType().replace(" AUTOINCREMENT", "")
else
currentDialectTest.dataTypeProvider.integerAutoincType()
withTables(excludeSettings = listOf(TestDB.MYSQL, TestDB.ORACLE, TestDB.MARIADB, TestDB.SQLITE), tables = *arrayOf(TestTable)) {
assertEquals("CREATE TABLE " + addIfNotExistsIfSupported() + "${"different_column_types".inProperCase()} " +
"(${"id".inProperCase()} $shortAutoIncType NOT NULL, \"${"name".inProperCase()}\" VARCHAR(42) PRIMARY KEY, " +
"(${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} NOT NULL, " +
"\"${"name".inProperCase()}\" VARCHAR(42) PRIMARY KEY, " +
"${"age".inProperCase()} ${currentDialectTest.dataTypeProvider.integerType()} NULL)", TestTable.ddl)
}
}
Expand Down

0 comments on commit 93536d4

Please sign in to comment.