ConstraintBuilder

Provides a Kotlin DSL for defining table-level constraints within a KQLiteTable definition.

This interface is the receiver for the KQLiteTable.constraints lambda in a table definition, allowing you to declaratively specify constraints like PRIMARY KEY, UNIQUE, CHECK, and FOREIGN KEY.

Following constraints can be created:

Example

object MyTable : KQLiteTable("my_table") {
val id = longColumn("id")
val email = textColumn("email").notNull()
val score = integerColumn("score")

override val constraints: (ConstraintBuilder.() -> Unit)? = {
// Define a named primary key
primaryKey(id)

// Define a unique constraint without name on the email column
unique(email).onConflict(Action.REPLACE)

// Define a check constraint by name that can be deleted later
constraint("score_validation").check { score GTE 0 }
}
}

Author

MOHAMMAD AZIM ANSARI

See also

Functions

Link copied to clipboard
abstract fun check(clause: Clause.() -> Unit): Constraint

Defines a CHECK constraint on the KQLiteTable column.

Link copied to clipboard
abstract fun constraint(name: String): ConstraintBuilder

Specifies a name for the Constraint that follows. Represents CONSTRAINT <name>.

Link copied to clipboard
abstract fun foreignKey(vararg column: KQLiteColumn<*>): ForeignKeyConstraint

Defines a FOREIGN KEY ForeignKeyConstraint on one or more columns, linking them to another KQLiteTable.

Link copied to clipboard
abstract fun primaryKey(vararg column: KQLiteColumn<*>): PrimaryKeyConstraint

Defines a PRIMARY KEY PrimaryKeyConstraint constraint on one or more KQLiteTable columns.

Link copied to clipboard
abstract fun unique(vararg column: KQLiteColumn<*>): UniqueConstraint

Defines a UNIQUE UniqueConstraint on one or more KQLiteTable columns.