use*_*920 4 kotlin kotlin-exposed
我在 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] = ?????????????
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该以与插入任何其他值相同的方式执行此操作 - 提供正确的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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3190 次 |
最近记录: |