插入后获取自动生成的 ID - ktor 暴露
Getting autogenerated id after insert - ktor exposed
我是 ktor 的新手,我正在尝试在 mysql table 中插入一行,其中包含 Ktor Exposed。 table定义如下:
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 – " + 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
}
}
最后我通过将 id
列编辑为 entity
列并将我的 table 定义为 IdTable<Int>
:
来解决
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
}
}
我是 ktor 的新手,我正在尝试在 mysql table 中插入一行,其中包含 Ktor Exposed。 table定义如下:
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 – " + 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
}
}
最后我通过将 id
列编辑为 entity
列并将我的 table 定义为 IdTable<Int>
:
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
}
}