Alex
Alex

Reputation: 1

Ktorm does not return null

I am writing my server in kotlin ktor and ktorme. There was a problem that the ctor does not return null values.

Here is the database query

return database.safeTransaction {
            it.appeals.filter { it.isDeleted eq false }.toList()
        }

Here is the dao class

val Database.appeals get() = this.sequenceOf(AppealDao)
object AppealDao : Table<Appeal>("appeals") {
    val id = long("id").primaryKey().bindTo { it.id }
    val userCreatorId = long("user_creator_id").references(UserDao) { it.userCreator }
    val userEmployeeId = long("user_employee_id").references(UserDao) { it.userEmployee }
    val statusId = long("status_id").references(StatusDao) { it.status }
    val title = varchar("title").bindTo{ it.title }
    val userDeleteId = long("user_delete_id").references(UserDao) { it.userDelete }
    val deleteReason = varchar("delete_reason").bindTo{ it.deleteReason }
    val isDeleted = boolean("is_deleted").bindTo { it.isDeleted }
    val dateCreate = jdbcTimestamp("date_create").bindTo { it.dateCreate }
}

Here is the entity

interface Appeal : Entity<Appeal> {
    companion object : Entity.Factory<Appeal>()
    val id: Long
    var userCreator: User
    var userEmployee: User?
    var status: Status
    var title: String
    var userDelete: User?
    var deleteReason: String?
    var isDeleted: Boolean
    val dateCreate: Timestamp
}

and as you can see below on the screen, I didn't get the value of null enter image description here

although they are enter image description here

for create sql tables, i use flyway

CREATE TABLE appeals(
  id BIGSERIAL NOT NULL PRIMARY KEY,
  user_creator_id bigint NOT NULL,
  user_employee_id bigint,
  status_id bigint NOT NULL ,
  title varchar NOT NULL ,
  user_delete_id bigint,
  delete_reason varchar NULL,
  is_deleted boolean NOT NULL,
  date_create timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY(user_creator_id) REFERENCES users(id),
  FOREIGN KEY(user_employee_id) REFERENCES users(id),
  FOREIGN KEY(status_id) REFERENCES statuses(id)
);

The official site says that if the variable is declared as an entity, then if there are no values, it will return null https://www.ktorm.org/api-docs/org.ktorm.entity/-entity/index.html

All code https://github.com/EnderWarik/TemplateItCompany-backend

Upvotes: 0

Views: 185

Answers (1)

Alex
Alex

Reputation: 1

I found a crutch solution, but I would still like to get a full-fledged

return database.safeTransaction {
            it.appeals.filter { it.isDeleted eq false }.map {
                Appeal{
                    id = it.id
                    userCreator = it.userCreator
                    this.status = it.status
                    userEmployee = it.userEmployee
                    title = it.title
                    userDelete = it.userDelete
                    deleteReason = it.deleteReason
                    isDeleted = it.isDeleted
                    dateCreate = it.dateCreate
                }
            }.toList()

Upvotes: 0

Related Questions