vespertide_core/schema/primary_key.rs
1use serde::{Deserialize, Serialize};
2
3/// Configuration for a primary key declared with the object syntax.
4///
5/// When `auto_increment` is `true` the column is generated as a serial / identity column
6/// (`PostgreSQL` `SERIAL`, `MySQL` `AUTO_INCREMENT`, `SQLite` `AUTOINCREMENT`).
7/// Use [`PrimaryKeySyntax::Object`] to pass this to a [`ColumnDef`].
8///
9/// [`ColumnDef`]: crate::schema::ColumnDef
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
12#[serde(rename_all = "snake_case")]
13pub struct PrimaryKeyDef {
14 /// When `true`, the column value is generated automatically by the database on insert.
15 #[serde(default)]
16 pub auto_increment: bool,
17}
18
19/// Inline primary key declaration on a [`ColumnDef`], supporting both shorthand and full syntax.
20///
21/// In JSON model files you can write either:
22/// - `"primary_key": true` (shorthand, no auto-increment)
23/// - `"primary_key": {"auto_increment": true}` (full object syntax)
24///
25/// [`ColumnDef`]: crate::schema::ColumnDef
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
28#[serde(rename_all = "snake_case", untagged)]
29pub enum PrimaryKeySyntax {
30 /// Shorthand: `true` marks the column as a primary key without auto-increment.
31 Bool(bool),
32 /// Full object syntax allowing `auto_increment` to be configured.
33 Object(PrimaryKeyDef),
34}