kotlinkotlin-exposed

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?


Solution

  • 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() }
    }