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

SchemaUtils.create doesn't use enum column's 'toDb' function #1475

Closed
terminalnode opened this issue Mar 16, 2022 · 1 comment
Closed

SchemaUtils.create doesn't use enum column's 'toDb' function #1475

terminalnode opened this issue Mar 16, 2022 · 1 comment

Comments

@terminalnode
Copy link
Contributor

Using Kotlin 1.6.10 and Exposed 0.37.3

When setting a default value for an enum column and using SchemaUtils.create, Exposed uses toString() instead of the toDb method.

Here's as small an example as I could come up with to recreate the issue:

object MyTable : LongIdTable(name = "my_table", columnName = "id") {
  val myEnum: Column<MyEnum> = customEnumeration(
    name = "my_enum",
    sql = "ENUM('${MyEnum.DEFAULT.dbString}')",
    fromDb = { MyEnum.values().first { e -> e.dbString == (it as String) } },
    toDb = { it.dbString }, // <-- this and the line below are the relevant parts
  ).default(MyEnum.DEFAULT)
}

enum class MyEnum(val dbString: String) {
  DEFAULT("the default value");
  override fun toString() = "the wrong string!"
}

fun main() {
  Database.connect(
    url = "jdbc:mysql://localhost:3306/test",
    driver = com.mysql.cj.jdbc.Driver::class.qualifiedName!!,
    user = "username", password = "password")

  transaction {
    SchemaUtils.create(MyTable)
  }
}

This will generate the following SQL:

CREATE TABLE IF NOT EXISTS my_table (id BIGINT AUTO_INCREMENT PRIMARY KEY, my_enum ENUM('the default value') DEFAULT 'the wrong string!' NOT NULL)

which throws the following exception:

org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.SQLSyntaxErrorException: Invalid default value for 'my_enum'
@Tapac
Copy link
Contributor

Tapac commented Apr 16, 2022

Released in 0.38.1

@Tapac Tapac closed this as completed Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants