foreignKey

abstract fun foreignKey(vararg column: KQLiteColumn<*>): ForeignKeyConstraint

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

This function initiates the definition of a FOREIGN KEY. You must chain it with the ForeignKeyConstraint.references function to specify the target table and columns. You can also specify actions for ForeignKeyConstraint.onDelete and ForeignKeyConstraint.onUpdate conflict.

This function is used within the constraints block of a KQLiteTable definition.

Example

object Tracks : KQLiteTable("tracks") {
val trackId = longColumn("TrackId").primaryKey()
val albumId = longColumn("AlbumId")
// ... other columns

override val constraints: (ConstraintBuilder.() -> Unit)? = {
foreignKey(albumId)
.references(Albums.albumId) // From Albums table
.onDelete(ForeignKeyAction.RESTRICT)
.onUpdate(ForeignKeyAction.CASCADE)
}
}

Author

MOHAMMAD AZIM ANSARI

Parameters

column

The columns to include in the foreign key.

See also