generated Always As
protected fun <T> KQLiteColumn<T>.generatedAlwaysAs(column: KQLiteColumn<*>, type: Generated? = null): KQLiteColumn<T>
Defines this column as a GENERATED ALWAYS AS whose value is computed from other columns.
Generated columns, also known as computed columns, store values that are automatically calculated based on an expression. This is useful for creating columns that derive their data from other fields in the same row, such as a full name concatenated from first and last name columns, or a calculated value.
Example:
object Users : KQLiteTable("users") {
val id = longColumn("id").primaryKey()
val firstName = stringColumn("first_name").notNull()
val lastName = stringColumn("last_name").notNull()
// A VIRTUAL generated column for the full name.
// The expression concatenates firstName, a space, and lastName.
val fullName =
stringColumn("full_name").generatedAlwaysAs(
firstName.CONCAT(" ").CONCAT(lastName),
)
// A STORED generated column to store the length of the first name.
val nameLength =
intColumn("name_length").generatedAlwaysAs(
LENGTH(firstName),
type = Generated.STORED,
)
}Content copied to clipboard
Return
The same KQLiteColumn instance, now marked as a generated column, to allow for further chaining.
Author
MOHAMMAD AZIM ANSARI
Parameters
column
The expression that computes the value.
type
The type of the generated column.