【问题标题】:Getting autogenerated id after insert - ktor exposed插入后获取自动生成的 id - ktor 暴露
【发布时间】:2020-11-27 10:12:53
【问题描述】:

我是 ktor 的新手,我正在尝试使用 Ktor Exposed 在 mysql 表中插入一行。该表定义如下:

object Posts : Table("wpv1_posts") {
    val id: Column<Int> = integer("ID").uniqueIndex()
    val author: Column<Int> = integer("post_author")
    val title = varchar("post_title", 50)
    val date: Column<DateTime> = datetime("post_date")
    val dateGMT: Column<DateTime> = datetime("post_date_gmt")
    val content = text("post_content")
    val excerpt = text("post_excerpt")
    val guid = text("guid")
    val postType = text("post_type")
    val toPing = text("to_ping")
    val pinged = text("pinged")
    val postContentFiltered = text("post_content_filtered")
    val postMimeType = text("post_mime_type")
    val postStatus = text("post_status")
    val postParent = integer("post_parent")
}

我可以使用插入语句插入行,但我无法检索插入的值,出现错误:

com.babacomarket.backend.model.database.Posts.ID is not in record set
override fun createSubscription(subscription: SubscriptionRequest): Int {
        return transaction {
            val currentDate = DateTime()
            val currentDateUtc = currentDate.withZone(DateTimeZone.UTC)
            val id = Posts.insert {
                it[title] = "Subscription &ndash; " + DateTime.now()
                    .toString(DateTimeFormat.forPattern(POST_TITLE_DATE_FORMATTER))
                it[author] = 1
                it[date] = currentDate
                it[dateGMT] = currentDateUtc
                it[postStatus] = "wc-active"
                it[postType] = "shop_subscription"
                it[content] = ""
                it[excerpt] = ""
                it[pinged] = ""
                it[toPing] = ""
                it[postContentFiltered] = ""
            }get Posts.id
            return@transaction id
        }
    }

【问题讨论】:

    标签: kotlin ktor kotlin-exposed


    【解决方案1】:

    最后我通过将id 列编辑为entity 列并将我的表定义为IdTable&lt;Int&gt; 来解决:

    object Posts : IdTable<Int>("wpv1_posts") {
        override val id: Column<EntityID<Int>> = integer("ID").entityId()
        /// other columns, truncated for brevity
    }
    

    插入变为:

    override fun createSubscription(metas: List<Meta>): Int {
        try {
            return transaction {
                val currentDate = DateTime()
                val currentDateUtc = currentDate.withZone(DateTimeZone.UTC)
                val id = Posts.insert {
                    it[title] = "title"
                } get Posts.id
                transaction { // update the row just created
                    Posts.update({ Posts.id eq id }) {
                        it[guid] = getGuid(id.value)
                    }
                }
                return@transaction id.value
            }
        } catch (e: Exception) {
            return -1
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 2011-04-03
      • 2019-12-10
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多