Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the current date in db using Exposed and Kotlin

I want to define a column of type date. This column should hold the current date of the resource created. How do i set this up? I'm new to kotlin.

In python, one would impletement it this way: date_created = db.Column(db.DateTime, default=db.func.current_timestamp())

What's the kotlin equivalent?

like image 726
edoc Avatar asked Jan 23 '26 18:01

edoc


1 Answers

If you want to just have a datetime column with a default value which will be evaluated on insert of a new record please use defaultExpression function:

object YourTable : IntIdTable() {
   val dateCreated = datetime("date_created").defaultExpression(CurrentDateTime)
}

If you want to generate datetime value on the client side:

object YourTable : IntIdTable() {
   val dateCreated = datetime("date_created").clientDefault{ DateTime.now() }
}
like image 191
Tapac Avatar answered Jan 26 '26 03:01

Tapac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!