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

no easy way to access column used as identity? #861

Closed
bb01100100 opened this issue Apr 5, 2020 · 0 comments · Fixed by #1731
Closed

no easy way to access column used as identity? #861

bb01100100 opened this issue Apr 5, 2020 · 0 comments · Fixed by #1731
Assignees

Comments

@bb01100100
Copy link

Hello,

I have a simple reference table (Currencies) that I want a natural primary key on, and no incrementing integer key:

object Currencies : IdTable<String>("currency") {
    val symbol : Column<String> = varchar("symbol",3)
    override val id: Column<EntityID<String>> = symbol.entityId()
    override val primaryKey = PrimaryKey(symbol)
}

I create an entity object:

class Currency(symbol: EntityID<String>) : Entity<String>(symbol) {
    companion object : EntityClass<String, Currency>(Currencies)
    var symbol by Currencies.symbol
}

However, if I try to refer to the symbol column I get an error:

val currency = Currency.get("USD")

println("Symbol property on Currency object is ${currency.symbol}")

.. Error: java.lang.IllegalStateException: Model.Currencies.symbol is not in record set

I then change the companion object to refer to the entity ID (Currencies.id instead of Currencies.symbol):

class Currency(symbol: EntityID<String>) : Entity<String>(symbol) {
    companion object : EntityClass<String, Currency>(Currencies)
    var symbol by Currencies.id
}

Then I can refer to the symbol column but I need to use the awkward currency.symbol.value to get the declared string value of the column...

val currency = Currency.get("USD")
println("Symbol column is a string, but here it is: ${currency.symbol::class.qualifiedName}")
println("I need to do currency.symbol.value in order for it to work: ${currency.symbol.value}")

Output:
  Symbol column is a string, but here it is: org.jetbrains.exposed.dao.DaoEntityID
  I need to do currency.symbol.value in order for it to work: USD

How do I configure the companion object so that I can refer to the symbol property without having to know that it's a primary key and use symbol.value to get to the declared column type of String?

If I use an integer incrementing column for Id then it works fine; but in this domain model I really want to use the natural key.

Thank you!

@joc-a joc-a linked a pull request Apr 25, 2023 that will close this issue
@joc-a joc-a self-assigned this Apr 25, 2023
@joc-a joc-a linked a pull request Apr 25, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants