(not available for MySQL and SQLite)

Let’s start with a very simple example of a table storing invoice numbers. We want the numbers to be automatically generated, starting with 1. To do that, we need to use sequences.

Adding a new sequence

Unlike the other model elements, sequences can be added only from Model Structure. This is because they are not visible in the model.

Sequence details

To enumerate invoices, our sequence will start at 1 and will be incremented by 1 on each call, just like a typical id number. We can also set a minimum or a maximum value for a sequence, as well as a cache and a cycle, but we won’t do that now.

Using a sequence

To make calls to the sequence (using PostgreSQL), we are going to set the default value of each table to nextval (invoice_number). What nextval(sequence_name) does is advance the sequence and return the new value – that’s why we don’t have to bother with having any duplicates.

If it’s still not enough, you can provide your sequence with an additional SQL script or set some additional properties specific for the database engine of your choice, here for PostgreSQL.

SQL Preview

SQL preview is available for sequences, you can see it for our example in the image below.