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

Can't seem to supply my own id for idtables #432

Closed
AllanWang opened this issue Nov 18, 2018 · 1 comment
Closed

Can't seem to supply my own id for idtables #432

AllanWang opened this issue Nov 18, 2018 · 1 comment
Assignees
Labels

Comments

@AllanWang
Copy link
Contributor

Exposed 0.11.2

It seems like creating a new "entity" and supplying the id results in a failure that no key was generated.

table:

object PrinterGroupTable : IdTable<String>("printer_group") {
    override val id = varchar("id", ID_SIZE).entityId()
    val name = varchar("name", NAME_SIZE).uniqueIndex()
    val loadBalancer = varchar("load_balancer", FLAG_SIZE)
}

execution:

 protected fun transactionInsert(body: T.(InsertStatement<EntityID<K>>) -> Unit): M? = transaction {
    try {
        val id = table.insertAndGetId(body)
        commit()
        getById(id.value)
    } catch (e: Exception) {
        null
    }
}

// Id and Name are type aliases for String
override fun create(id: Id, name: Name, group: PrinterGroup): Printer? = transactionInsert {
    it[table.id] = EntityID(id, table)
    it[table.name] = name
    it[table.group] = group.id
}

output when calling create:

20:17:53.249 [main] DEBUG Exposed - INSERT INTO printer_group (id, load_balancer, name) VALUES ('testGroupId0', 'testQueueManager0', 'testGroupName0')
Exception java.lang.IllegalStateException: No key generated
@AllanWang
Copy link
Contributor Author

AllanWang commented Nov 18, 2018

To add more details, this works:

protected fun transactionInsert(id: K, body: T.(InsertStatement<Number>) -> Unit): M? = transaction {
    try {
        table.insert {
            body(it)
            it[table.id] = EntityID(id, table)
        }
        getById(id)
    } catch (e: Exception) {
        null
    }
}

but this does not:

protected fun transactionInsert(id: K, body: T.(InsertStatement<Number>) -> Unit): M? = transaction {
    try {
        val insertId = table.insert {
            body(it)
            it[table.id] = EntityID(id, table)
        } get table.id
        getById(insertId!!.value)
    } catch (e: Exception) {
        null
    }
}

Ideally, I'd like to remove the id parameter from my method, as it will either be passed through the body or generated with client defaults. With the current implementation, I also have to make sure that my caller does not pass an id, or there will be an exception caused by duplicate sets


Snapshot of my project, available for testing:

https://github.com/AllanWang/Prim/blob/0a14e38f00fc1b45563d1dd7f2196eebfca770b7/printer-sql/src/main/kotlin/ca/allanwang/prim/printer/sql/PrinterSql.kt#L63

@Tapac Tapac self-assigned this Nov 19, 2018
@Tapac Tapac added the bug label Nov 19, 2018
@Tapac Tapac closed this as completed Feb 9, 2019
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