Table types
In Exposed, the Table class is the core abstraction for defining database tables. This class provides methods to define various column types, constraints, and other table-specific properties.
Auto-incrementing ID column tables
Apart from the core Table class, Exposed provides the base IdTable class and its subclasses through the DAO API.
The IdTable class extends Table and is designed to simplify the definition of tables that use a standard id column as the primary key. These tables can be declared without explicitly including the id column, as IDs of the appropriate type are automatically generated when creating new table rows.
IdTable and its subclasses are located in the org.jetbrains.exposed.v1.core.dao.id package of the exposed-core module.
IntIdTableIntID columnLongIdTableLongID columnUIntIdTableUIntID columnULongIdTableULongID columnUuidTablekotlin.uuid.UuidID columnUUIDTablejava.util.UUIDID column. Available from the packageorg.jetbrains.exposed.v1.core.dao.id.java.
The following example represents a table with custom columns sequel_id, name, and director:
The IntIdTable class automatically generates an auto-incrementing integer id column, by chaining .autoIncrement(), which serves as the primary key for the table. When the table is created, it corresponds to the following SQL query:
If the underlying database supports sequence creation, an argument can be passed to the sequenceName parameter of any compatible IdTable. If the example above is switched to IntIdTable(sequenceName = "id_seq"), the following SQL statements will be generated on table creation:
Auto-generating Uuid
The UuidTable class automatically generates a random Kotlin Uuid instance on the client side before an insert, as long as a value is not manually assigned to the column during the operation. By default, this class relies on calls to .autoGenerate(), which returns a version 4 Uuid from Uuid.generateV4().
If you prefer that the UuidTable class uses Uuid.generateV7() to automatically generate time-based Uuids, this can be accomplished by providing an argument to the uuidVersion constructor parameter, like UuidTable(uuidVersion = UuidVersion.V7). This would be the equivalent of chaining .autoGenerate(UuidVersion.V7) directly on a registered Uuid column.
The Java UUIDTable class automatically generates a random Java UUID instance by calling UUID.randomUUID() before the insert operation.
For more information on defining and configuring tables in Exposed, see Working with tables.
Composite ID table
To define multiple columns as part of the primary key and ID, use CompositeIdTable and mark each composite column using .entityId(). Each component column will be available for CRUD operations either individually (as for any standard column) or all together as part of the id column:
If any of the key component columns have already been marked by .entityId() in another table, they can still be identified using addIdColumn(). This might be useful for key columns that reference another IdTable:
Custom column type table
To define a custom column type as the primary key and ID, use a typed IdTable directly and override the id column, as shown in the following example:
In the definition of DirectorsCustomTable, the id field is of type Column<EntityID<String>>, which will hold String values with a length of up to 32 characters. Using the override keyword indicates that this id column is overriding the default id column provided by IdTable.
Once all columns are defined, the id column is explicitly set as the primary key for the table, using the override keyword once again.