【问题标题】:How to insert and return an inserted row in Exposed? [duplicate]如何在 Exposed 中插入和返回插入的行? [复制]
【发布时间】:2022-01-22 17:18:26
【问题描述】:

我有一个插入新行并返回其 ID 的代码:

transaction {
    val id = Comments.insert {
        it[this.postId] = postId
        it[msg] = comment.msg
        it[this.createdBy] = createdBy
    } get Comments.id
}

但是,我怎样才能返回所有值,而不仅仅是 ID?在 SQL 中,我会写如下内容:

INSERT INTO comments (post_id, msg, created_by)
VALUES (?, ?, ?)
RETURNING id, post_id, msg, created_by, created_at;

【问题讨论】:

  • @МихаилНафталь 是的,这就是我需要的!谢谢!我在 Google 中找不到该主题,这有点奇怪。

标签: kotlin kotlin-exposed


【解决方案1】:

Exposed DSL 目前不支持指定自定义 RETURNING 子句。

这个问题有一些解决方法: https://github.com/JetBrains/Exposed/issues/1271

如果您愿意运行本机 SQL,那么方法如下: Is there a way to run raw sql with Kotlin's Exposed library

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    相关资源
    最近更新 更多