Exposed provides support for array data types, allowing you to store and manipulate arrays of various data types in your database. PostgreSQL and H2 databases support explicit array types, with PostgreSQL offering additional support for multidimensional arrays.
Supported types
The exposed-core module supports two main ways to define array columns using the .array() function:
Basic array definition using type inference for supported types:
val memberIds = array<UUID>("member_ids")
Array definition with explicit column type for more control or custom types:
val deadlines = array<LocalDate>("deadlines", KotlinLocalDateColumnType()).nullable()
Using explicit column types prevents exceptions when Exposed cannot automatically determine the appropriate mapping.
Basic usage
Array columns store and retrieve data as Kotlin List objects, providing a natural way to work with array data in your code.
You can define array columns within a table definition as follows:
// Simple array table definition
object SimpleArrays : Table("teams") {
val memberIds = array<UUID>("member_ids")
val memberNames = array<String>("member_names")
val budgets = array<Double>("budgets")
}
// Array with explicit column type table definition
object AdvancedArrays : Table("teams") {
val memberNames = array<String>("member_names", VarCharColumnType(colLength = MEMBER_NAME_LENGTH))
val deadlines = array<LocalDate>("deadlines", KotlinLocalDateColumnType()).nullable()
val expenses = array<Double?>("expenses", DoubleColumnType()).default(emptyList())
}
Here's an example of inserting data into array columns: