Declare sequence / identity start value and increment in entity metadata
Is your feature request related to a problem? Please describe.
There is no way to declare the start value (or increment) of an autoincrement / identity column in entity metadata. Today the only option is raw SQL in a migration (alter sequence ... restart with 1000, alter table ... auto_increment = 1000).
This has two practical problems:
- Anything built from metadata ignores it:
schema:create,schema:refresh,orm.schema.refreshDatabase()in tests. Every test setup has to repeat the raw SQL after the schema is created. - The value is invisible in the model, so it can drift between environments and nothing reports it.
This was asked in #7158. @B4nan's questions there were: how should it work, what about schema diffing, and should it be respected only when the sequence is created? The proposal below tries to answer them.
The closest thing that works today has a destructive side effect. On Postgres, generated is passed through into the DDL, so this creates the column correctly:
id: { type: 'number', primary: true, generated: 'identity (start with 1000)' }
// create table "thing" ("id" int generated always as identity (start with 1000) not null primary key, ...)
// first insert gets id 1000But introspection reads the column back as plain identity, so the very next getUpdateSchemaSQL() on an unchanged schema wants to drop and re-add the primary key:
alter table "thing" drop column "id";
alter table "thing" add "id" int generated always as identity (start with 1000) not null primary key;(Reproduced on 7.2.1 with @mikro-orm/pglite.) generated: 'by default as identity (start with 1000)' doesn't work either, because only the exact string 'by default as identity' is special-cased, so it renders as generated always as by default as identity (...).
Describe the solution you'd like
A sequence option on autoincrement properties (decorators and defineEntity):
@PrimaryKey({ sequence: { startWith: 1000, incrementBy: 1 } })
id!: number;
@Property({ autoincrement: true, sequence: { startWith: 10000 } })
ref!: number;Semantics per driver:
| Driver | On create | Diff on change |
|---|---|---|
PostgreSQL, identity (generated: 'identity' / 'by default as identity') |
generated ... as identity (start with 1000 increment by 1) |
Compare with information_schema.columns.identity_start / identity_increment, emit alter table ... alter column ... set start with X set increment by Y |
PostgreSQL, serial (the default) |
alter sequence "thing_id_seq" start with 1000 increment by 1 restart right after create table (the table is empty at that point) |
Compare with pg_sequences.start_value / increment_by, emit alter sequence ... start with X increment by Y |
| MySQL / MariaDB | create table ... auto_increment = 1000 |
None. information_schema.tables.AUTO_INCREMENT is "the next AUTO_INCREMENT value", not a declared start, so it can't be diffed. Create-only. |
| SQLite (AUTOINCREMENT tables) | insert into sqlite_sequence (name, seq) values ('thing', 999) after create |
None. Create-only. |
| MSSQL | identity(1000, 1) instead of the hardcoded identity(1,1) |
None. SQL Server can't alter the identity property of an existing column (the schema helper already works around this). Create-only. |
Answers to the questions in #7158:
- Only on create? Always on create. On Postgres the diff can also compare the declared values with the introspected ones. Per the Postgres docs,
ALTER SEQUENCE ... START WITH"changes the recorded start value of the sequence. This has no effect on the current sequence value", so the diff never touches existing data. Moving the current value of an existing sequence (restart) stays an explicit data-level action in a migration. - Schema diffing: the start/increment become part of the column's sequence metadata and are compared like defaults or types. Drivers that can't read back a declared start (MySQL, SQLite, MSSQL) skip it in the diff, so there are no spurious migrations.
Describe alternatives you've considered
- Raw SQL in a migration: works for migrated databases, but not for
schema:create/refreshDatabase(), and the value isn't visible in the model. generated: 'identity (start with N)': creates the right DDL, but every later diff drops and re-adds the PK column (see above).default: raw("nextval('custom_seq')")with a hand-made sequence: the ORM doesn't create or track the sequence, so it has to be created separately in every environment.
Additional context
Prior art:
- EF Core:
modelBuilder.HasSequence<int>("OrderNumbers").StartsAt(1000).IncrementsBy(5); SQL ServerUseIdentityColumn(seed, increment); NpgsqlHasIdentityOptions(startValue: 100) - JPA / Hibernate:
@SequenceGenerator(initialValue = 1000, allocationSize = ...) - Drizzle:
pgSequence('name', { startWith: 100, increment: 2 }),generatedAlwaysAsIdentity({ startWith: 100 })
Source: mikro-orm/mikro-orm