Skip to main content

IDENTITY

Constant IDENTITY 

Source
pub const IDENTITY: ColumnMarker;
Expand description

Creates a GENERATED ... AS IDENTITY column.

§Syntax

  • identity or identity(always): GENERATED ALWAYS. The database rejects explicit values unless the insert says OVERRIDING SYSTEM VALUE, so the insert model has no setter for the column.
  • identity(by_default): GENERATED BY DEFAULT. An explicit value wins.
  • Sequence options may follow the mode: start, increment, min_value, max_value, cache, cycle, as in identity(always, start = 100, cycle).

§Examples

// GENERATED ALWAYS AS IDENTITY - strictest mode
#[column(identity(always), primary)]
id: i64,

// GENERATED BY DEFAULT AS IDENTITY - allows override
#[column(identity(by_default), primary)]
id: i64,

§Technical Details

PostgreSQL’s identity columns are SQL-standard compliant, unlike SERIAL. Use ALWAYS for auto-generated IDs, BY DEFAULT when you need to occasionally set values.

See: https://www.postgresql.org/docs/current/ddl-identity-columns.html