使用 KTOR 和 EXPOSED 具有表关系的 CRUD

Posted

技术标签:

【中文标题】使用 KTOR 和 EXPOSED 具有表关系的 CRUD【英文标题】:CRUD with table relationship using KTOR and EXPOSED 【发布时间】:2019-10-24 04:50:57 【问题描述】:

我在使用表之间的关系时遇到了 KTOR 和 EXPOSED 问题。 我将我的服务配置如下:

class LegalPersonService 

    suspend fun findAll(): List<LegalPerson> = dbQuery 
        LegalPersons.selectAll().map  toLp(it) 
    

    suspend fun insert(lp: LegalPerson, ph: Phone) = dbQuery 
        LegalPersons.insert 
            it[id] = lp.id
            it[internalId] = lp.internalId
            it[companyId] = lp.companyId
            it[active] = lp.active
            it[tradeName] = lp.tradeName
            it[fantasyName] = lp.fantasyName
            it[email] = lp.fantasyName
            it[cnpj] = lp.cnpj
            it[stateRegistration] = lp.stateRegistration
            it[muninipalRegistration] = lp.muninipalRegistration
            it[address] = lp.address
        .let 
            Phones.insert 
                it[id] = ph.id
                it[internalId] = ph.internalId
                it[phone] = ph.phone
            
        
    

    private fun toLp(row: ResultRow): LegalPerson =
        Phone(
            id = row[Phones.id],
            internalId = row[Phones.internalId],
            phone = row[Phones.phone]
        ).let 
            LegalPerson(
                id = row[LegalPersons.id],
                internalId = row[LegalPersons.internalId],
                companyId = row[LegalPersons.companyId],
                active = row[LegalPersons.active],
                tradeName = row[LegalPersons.tradeName],
                fantasyName = row[LegalPersons.fantasyName],
                email = row[LegalPersons.email],
                cnpj = row[LegalPersons.cnpj],
                stateRegistration = row[LegalPersons.stateRegistration],
                muninipalRegistration = row[LegalPersons.muninipalRegistration],
                address = row[LegalPersons.address]
            )
        

还有我的模特:

// *** LEGAL PERSONS ***

data class LegalPerson(
    val id: UUID,
    val internalId: Long,
    val companyId: UUID,
    val active: Boolean,
    val tradeName: String,
    val fantasyName: String,
    val email: String,
    val cnpj: String,
    val stateRegistration: String,
    val muninipalRegistration: String,
    val address: UUID
)

object LegalPersons : Table("person.legal_person") 
    val id: Column<UUID> = uuid("id").autoIncrement().primaryKey()
    val internalId: Column<Long> = long("internal_id").autoIncrement()
    val companyId: Column<UUID> = uuid("company_id")
    val active: Column<Boolean> = bool("active")
    val tradeName: Column<String> = varchar("trade_name", 100)
    val fantasyName: Column<String> = varchar("fantasy_name", 100)
    val email: Column<String> = varchar("email", 100)
    val cnpj: Column<String> = varchar("cnpj", 18)
    val stateRegistration: Column<String> = varchar("state_registration", 20)
    val muninipalRegistration: Column<String> = varchar("municipal_registration", 20)
    val address: Column<UUID> = uuid("address")


// *** PHONES ***

data class Phone(
    val id: UUID,
    val internalId: Long,
    val phone: UUID
)

object Phones : Table("person.phone_legal_person") 
    val id: Column<UUID> = reference("id", LegalPersons.id).primaryKey()
    val internalId: Column<Long> = long("internal_id").autoIncrement()
    val phone: Column<UUID> = uuid("phone")

但是当我尝试插入数据时出现此错误:

ERROR Application - Unhandled: POST - /api/clients/lp/
io.ktor.gson.UnsupportedNullValuesException: Receiving null values is not supported

有人可以帮忙吗?我使用的是 DLS 而不是 DAO。 我遇到了困难,因为文档仍在制作中。

【问题讨论】:

错误信息提示post请求正文中的一个不可为空的值实际上为null,因此接收失败。验证您的请求和 ktor 服务器代码。 【参考方案1】:

读了一点之后,我发现出了什么问题。 我的服务如下所示:

suspend fun insert(lp: LegalPerson) = dbQuery 
        val ids = UUID.randomUUID()
        LegalPersons.insert 
            it[id] = ids
            it[companyId] = lp.companyId
            it[active] = lp.active
            it[tradeName] = lp.tradeName
            it[fantasyName] = lp.fantasyName
            it[email] = lp.fantasyName
            it[cnpj] = lp.cnpj
            it[stateRegistration] = lp.stateRegistration
            it[muninipalRegistration] = lp.muninipalRegistration
            it[address] = lp.address
        

        Phones.batchInsert(lp.phones)  phone ->
            this[Phones.id] = ids
            this[Phones.phone] = phone.phone

        
    

【讨论】:

以上是关于使用 KTOR 和 EXPOSED 具有表关系的 CRUD的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 Exposed 连接到 Google Cloud SQL

Kotlin ktor 暴露 DSL 插入参考

具有 keycloak 的 ktor 中相同 oauth 设置上的多个 urlProviders

如何使用 Ktor 和 Kotlin 下载带有进度指示器的大文件?

Kotlin Exposed 获取更新的行数

Kotlin Exposed 使用引用创建实体