Search code examples
kotlinserverktorkotlin-exposed

Kotlin Server Ktor Exposed: how to nullable optional Fields


I have this function on my Ktor + Exposed App.

override suspend fun createNewCourse(course: CourseModel): Flow<CourseModel> {
    transaction {
        CoursesTable.insert {
            it[requiredCourseId] = course.requiredCourse?.id!!
            it[category] = course.category?.id!!
            it[isPopular] = course.isPopular == true
            it[position] =  course.position
            it[nameEN] = course.name.en
            it[warningEN] = course.warning.en
        }
    }

It doesn't compile. Sometimes some variables (like "warningEN") can be null and I dont want to insert nothing for this field.

How to make it?

Type mismatch. Required:TypeVariable(S) Found: String?


Solution

  • My solution:

    it[position] =  course.position?.let { course.position!! } ?: null