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

Impossible to set default value with nullable column? #474

Closed
silospen opened this issue Jan 23, 2019 · 1 comment
Closed

Impossible to set default value with nullable column? #474

silospen opened this issue Jan 23, 2019 · 1 comment
Assignees
Labels

Comments

@silospen
Copy link

silospen commented Jan 23, 2019

For a column which is both nullable and has a default value, it should be possible to set 3 different values:

  • A value
  • NULL
  • The default value

In the insert SQL, that would equate to:

  • SQL containing a value
  • SQL containing an explicit NULL
  • SQL with the field missing

As far as I can tell in exposed there's no way to express the third variant and so it's impossible to get a default value. Am I missing a setting somewhere or are you using NULL to indicate both the absence of data and null data?

Thanks!

import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction

object ExampleTable : Table() {
    val text = varchar("text", 50).default("default-text-value").nullable()
}

fun main(args: Array<String>) {
    Database.connect("jdbc:h2:mem:", "org.h2.Driver")

    transaction {
        SchemaUtils.create(ExampleTable)
        ExampleTable.insert { it[text] = "first-text-value" }
        ExampleTable.insert { it[text] = null }
        ExampleTable.insert {  }
        ExampleTable.selectAll().forEach(::println)
    }

    /**
     * EXPECTED:
     *  - "first-text-value"
     *  - NULL
     *  - "default-text-value"
     */

    /**
     * ACTUAL:
     *  - "first-text-value"
     *  - NULL
     *  - NULL
     */

}
@Tapac Tapac self-assigned this Jan 29, 2019
@Tapac Tapac added the bug label Jan 29, 2019
@Tapac
Copy link
Contributor

Tapac commented Jan 29, 2019

Definitely empty insert should work, but it doesn't. Will be fixed on the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants