Exposed 0.55.0 Help

Working with Sequence

A sequence is a database object that automatically generates integer values in sequential order. It is particularly useful in generating unique identifiers and primary keys.

Define a sequence

To define a sequence in Exposed, use the Sequence class:

val myseq = Sequence("my_sequence") // my_sequence is the sequence name.

Several parameters can be specified to control the properties of the sequence:

private val myseq = Sequence( name = "my_sequence", startWith = 4, incrementBy = 2, minValue = 1, maxValue = 10, cycle = true, cache = 20 )

Create a sequence

To create a sequence, use the createSequence() method provided by SchemaUtils:

SchemaUtils.createSequence(myseq)

Drop a sequence

To drop a sequence, use the dropSequence() method provided by SchemaUtils:

SchemaUtils.dropSequence(myseq)

Access the next value

You can access the next value in a sequence by using the nextIntVal() function:

val nextVal = myseq.nextIntVal() val id = StarWarsFilms.insertAndGetId { it[id] = nextVal it[name] = "The Last Jedi" it[sequelId] = 8 it[director] = "Rian Johnson" }
val firstValue = StarWarsFilms.select(nextVal).single()[nextVal]
Last modified: 25 September 2024