https://kotlinlang.org logo
#exposed
Title
# exposed
a

aazavoykin

02/09/2023, 7:14 AM
Hello, evenryone! I`m a novice with a little question: how could I create a table`s column with a check constraint between values of two columns like in the example (
"limit" >= balance
):
Copy code
CREATE TABLE IF NOT EXISTS account (
  id          uuid PRIMARY KEY,
  balance     DECIMAL(16,2) NOT NULL DEFAULT 0 CHECK (balance >= 0),
  "limit"     DECIMAL(16,2) NOT NULL DEFAULT 0 CHECK ("limit" >= balance)
);
How could I get the current value of column here?
Copy code
object AccountTable : Table("account") {
...
val limit: Column<BigDecimal> = decimal("limit", 16, 2)
    .default(BigDecimal.ZERO)
    .check {it.greaterEq(??????)}
}
s

Sergey Shumov

02/09/2023, 8:08 AM
Copy code
object AccountTable : UUIDTable("account") {
    val balance = decimal("balance", 16, 2)
        .default(BigDecimal.ZERO)
        .check { it greaterEq BigDecimal.ZERO }
    val limit = decimal("limit", 16, 2)
        .default(BigDecimal.ZERO)
        .check { GreaterEqOp(it, balance) }
}
a

aazavoykin

02/09/2023, 8:10 AM
Thank you! That`s exactly what I needed
16 Views