https://kotlinlang.org logo
#exposed
Title
# exposed
l

Luca Piccinelli

04/24/2021, 2:00 PM
@tapac I have this code
Copy code
object BuddyTable: IdTable<Int>("toh.buddy"){
    override val id = integer("id").autoIncrement("toh.buddy_seq_id").entityId()
    val name = varchar("name", 50)
    val hero = reference("hero_id", HeroTable)
}

class BuddyDao(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<BuddyDao>(BuddyTable)

    var name by BuddyTable.name
    var hero by HeroDao referencedOn BuddyTable.hero
}

override fun save(entity: Buddy): Int = transaction {
        BuddyTable.insert {
            it[name] = entity.name
            it[hero] = entity.hero.id
        }[BuddyTable.id].value
}
In postgres it looks like it is not using the sequence to generate the new id (also if i don't override the id field) Prefixing or not with the schema "toh" doesn't change. The error is
Copy code
Detail: Key (id)=(3) already exists.. Statement(s): INSERT INTO toh.buddy (hero_id, "name") VALUES (?, ?)

org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "buddy_pkey"
  Detail: Key (id)=(3) already exists.
It does 3 transaction attempts with ids 1, 2 and 3
t

tapac

04/24/2021, 2:02 PM
It's known issue (https://github.com/JetBrains/Exposed/issues/1209) the fix will be available today or tomorrow
👍 1
🙏 1
l

Luca Piccinelli

04/24/2021, 2:03 PM
ah ok... looking at the code I thought it should already work
there exist a workaround to use autoincrement with postgres?
sorry, in my case it was my fault, not a bug. In the test I was assigning the ids manually (then the sequence didn't get incremented), while in the repository I was using the sequence.
11 Views