Hi question about <auto-fill created and updated c...
# exposed
y
Hi question about auto-fill created and updated columns: It seems like if I don’t use the Entity to update the table (using the DSL API) then the updated field is not treated As I understand it Entity hooks only apply to entity related changes and not to changes directly to the table using the DSL API. is there a way to do the same when using the DSL API to also auto-fill the updated column?
t
You can define default value on column.
y
but that will only work when I insert for the first time, it wouldn’t work when I update a row in the table
t
So you want overwrite existing value with new one?
y
yes, I want to update the
updated
column on every change to that row
the same way it is happening with entity hooks but with table DSL
t
I think it's only possible with a very tricky way with StatementInterceptors or creating your own inheritor of
UpdateStatement
which will add required columns into every batch.
y
I’ll look into it
wrote this:
Copy code
fun <T: BaseLongIdTable> T.update(where: (SqlExpressionBuilder.()->Op<Boolean>)? = null, limit: Int? = null, body: T.(UpdateStatement)->Unit): Int {
    val query = UpdateStatement(this, limit, where?.let { <http://SqlExpressionBuilder.it|SqlExpressionBuilder.it>() })
    body(query)
    query[this.updatedAt] = LocalDateTime.now()
    return query.execute(TransactionManager.current())!!
}
but then I have to make sure that I call the right update method, meaning the one I wrote and not the one in the Exposed library. seems like it can cause confusion any tips?
f
@tapac Can it be really done also with
StatementInterceptors
? Because if I understand correctly, I am not able to modify arguments of current statement and getting exception
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 3.
Hmm Maybe I got it. 1. I added new value to UpdateStatement by
statement[column] = value
2. I get index of
Pair(column, value)
from UpdateStatement 3. add value to
(context.args as ArrayList)
to specified index. But it doesn't look to be good usage of exposed at all (: