Search code examples
kotlin-exposed

How set id via Exposed


I create object and dao class for work with sql

object UserTable : IdTable<Int>("User") {
    val parameters = reference("search_parameters_id", SearchParametersTable)

    override val id = integer("id").entityId()
    override val primaryKey = PrimaryKey(id)
}

class User(id: EntityID<Int>) : Entity<Int>(id) {
    companion object : EntityClass<Int, User>(UserTable)

    var searchParameters by SearchParameters referencedOn UserTable.parameters
}

But I cann't set id, beacause id is val


Solution

  • Do you mean that you want to insert a record with an arbitrary id value? If yes, you can write like below.

    val newId = 10
    User.new(newId) {
        // set values to other columns
    }