ktorm
ktorm copied to clipboard
Entities: Allow References to Table with compound primary keys
Schema example:
interface FinancialAccount : Entity<FinancialAccount> {
companion object : Entity.Factory<FinancialAccount>()
val accNumber: Long
val finInst: FinancialInstitution?
val finAccType: FinancialAccountType?
}
interface FinancialTransaction : Entity<FinancialTransaction> {
companion object : Entity.Factory<FinancialTransaction>()
val transactionId: Long
val finAcc: FinancialAccount?
val aliasName: String
}
object FinancialAccounts : Table<FinancialAccount>("financial_account") {
val accNumber = long("accnumber").primaryKey().bindTo { it.accNumber }
val finInstCode = varchar("fininstcode").primaryKey().references(FinancialInstitutions) { it.finInst }
val finAccType = varchar("finacctype").references(FinancialAccountTypes) { it.finAccType }
}
object FinancialTransactions : Table<FinancialTransaction>("Financial_Transaction") {
val transactionId = long("transactionid").primaryKey().bindTo {it.transactionId}
val accNumber = long("accnumber").references(FinancialAccounts) {it.finAcc}
val finInstCode = varchar("fininstcode").references(FinancialAccounts) {it.finAcc}
val aliasName = varchar("aliasname").bindTo {it.aliasName}
}
When trying to database.financialTransactions.first() I got the following exception
Caused by: java.lang.IllegalStateException: Cannot reference the table 'financial_account' because it has compound primary keys.
at org.ktorm.schema.BaseTable.checkReferencePrimaryKey(BaseTable.kt:267)
at org.ktorm.schema.BaseTable.doBindInternal(BaseTable.kt:222)
This happens because the entity FinancialAccount has a two primaries keys (Is a composite key). It would be nice to support this, is a common scenario in a database
Thank you, will consider adding this feature.