gpt4 book ai didi

kotlin - 如何在 Kotlin 中插入带有外键的记录?

转载 作者:行者123 更新时间:2023-12-02 12:47:11 25 4
gpt4 key购买 nike

我在 Kotlin Exposed 文档中找不到如何使用外键插入记录的方法:

object DocumentTable : IntIdTable() {
val description = varchar("desc", 200)
}

object TransactionTable : IntIdTable() {
val amount = long("amount")
val documentId = reference("documentId", DocumentTable.id)
}

fun createTrans(amount: Long, document: Document) {
transaction {
TransactionTable.insert {
it[this.amount] = amount
it[this.documentId] = ?????????????
}
}
}

最佳答案

您应该像插入任何其他值一样执行此操作 - 提供正确的 documentId:

transaction {
val docId = DocumentTable.select { /*your condition here */ }.single()[DocumentTable.id]
// or if you want to construct your id from scratch (be sure that you have such record in a database or it will fail with exception)
val docId = EntityID(123, DocumentTable)

TransactionTable.insert {
it[this.amount] = amount
it[this.documentId] = docId
}
}

关于kotlin - 如何在 Kotlin 中插入带有外键的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com