Exposed: Composite unique constraint

Created on 30 Jan 2018  路  5Comments  路  Source: JetBrains/Exposed

Can we do something like this with Exposed?

CREATE TABLE someTable (
    id serial primary key,
    col1 int NOT NULL,
    col2 int NOT NULL,
    unique (col1, col2)
)

or

CREATE TABLE someTable (
    col1 int NOT NULL,
    col2 int NOT NULL,
    primary key (col1, col2)
)
documentation

Most helpful comment

object SomeTable : Table() {
    val col1 = integer("col1").primaryKey(0) // case 2 - composite PK
    val col2 = integer("col2").primaryKey(1) // case 2 - composite PK
    init {
        index(true, col1, col2) // case 1 - Unique index
    }
}

All 5 comments

object SomeTable : Table() {
    val col1 = integer("col1").primaryKey(0) // case 2 - composite PK
    val col2 = integer("col2").primaryKey(1) // case 2 - composite PK
    init {
        index(true, col1, col2) // case 1 - Unique index
    }
}

@Tapac I didn't understand what does the following .primaryKey(0) mean? Does index 0 stand for the column col1? Could you elaborate on this?

@terancet , that means what col1 will be the first column in a primary key.

Given this is still advised solution for the problem, any suggestion how to handle DAO on such table (where two fields used are used as combined ID)?

Highly recommend adding this to Wiki 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BugsBunnyBR picture BugsBunnyBR  路  3Comments

supertote picture supertote  路  3Comments

coolemza picture coolemza  路  3Comments

gcscaglia picture gcscaglia  路  3Comments

yuri-li picture yuri-li  路  3Comments