unique
Defines a UNIQUE UniqueConstraint on one or more KQLiteTable columns.
A UNIQUE constraint ensures that all values in a column or a combination of columns are unique. This constraint can be applied to a single column or multiple columns (a composite unique key). While similar to a PRIMARY KEY, a table can have multiple UNIQUE constraints, and they can permit NULL values (though how multiple NULLs are handled can vary between database systems; SQLite allows multiple NULLs).
Use this function within the constraints block of a KQLiteTable definition. You can specify the conflict resolution strategy by chaining UniqueConstraint.onConflict on the result.
Example (Single Column Unique Constraint)
object Users : KQLiteTable("users") {
val userId = longColumn("UserId").primaryKey()
val email = textColumn("Email").notNull()
override val constraints: (ConstraintBuilder.() -> Unit)? = {
constraint("unique_email").unique(email).onConflict(Action.REPLACE)
}
}Example (Composite Unique Constraint)
object CourseRegistrations : KQLiteTable("course_registrations") {
val studentId = longColumn("StudentId")
val courseId = longColumn("CourseId")
override val constraints: (ConstraintBuilder.() -> Unit)? = {
// A student can only register for a specific course once.
constraint("unique_student_course").unique(studentId, courseId)
}
}Return
A UniqueConstraint object, allowing for further configuration like specifying an UniqueConstraint.onConflict clause.
Author
MOHAMMAD AZIM ANSARI
Parameters
The columns to be part of the unique constraint.