Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Read-only columns support

Open crazyproger opened this issue 9 years ago • 3 comments
trafficstars

By read-only I mean columns that generated/updated by database( for example ON UPDATE for mysql, or generated column, or modified by trigger). Such columns should not be used in update queries. How about such syntax:

object ConfigTable : IntIdTable() {
    val key = varchar("name", 255)
    val value = varchar("value", 255)
    val created = readonly(datetime("created"))
    val modified = readonly(timestamp("modified"))
}

And for entities all fields that referenced by readonly table-fields can only be val:

class ConfigEntity(id: EntityID<Int>) : IntEntity(id) {
    var key by ConfigTable.key
    var value by ConfigTable.value
// OK
    val created by ConfigTable.created
// Compile-time error
    var modified by ConfigTable.modified

    companion object : IntEntityClass<ConfigEntity>(ConfigTable)
}

crazyproger avatar Oct 25 '16 11:10 crazyproger

Any updates on this @Tapac? Has anybody found a workaround for adding this?

Aditya94A avatar Nov 18 '19 10:11 Aditya94A

It feasible by declaring your own StatementInterceptor where you will check that some columns are absent from UpdateStatement

Tapac avatar Nov 18 '19 20:11 Tapac

We managed to use something like this:

object ConfigTable : IntIdTable() {
    val key = varchar("name", 255)
    val value = varchar("value", 255)
    val created = datetime("created")
    val modified = timestamp("modified")
}

class ConfigEntity(id: EntityID<Int>) : IntEntity(id) {
    var key by ConfigTable.key
    var value by ConfigTable.value

    val created: LocalDateTime
        get() = _created!!

    val modified: Instant
        get() = _modified!!

    private val _created by ConfigTable.created.nullable()
    private val _modified by ConfigTable.modified.nullable()

    companion object : IntEntityClass<ConfigEntity>(ConfigTable)
}

solution-al avatar Feb 08 '23 10:02 solution-al

Since version 0.44.0, a column can be flagged as generated/updated by the database using Column.databaseGenerated().

bog-walk avatar Jun 18 '24 15:06 bog-walk