Skip to main content

runique/migration/primary_key/
mod.rs

1//! Primary key definition — type (i32, i64, UUID), auto-increment, and SeaQuery generation.
2use sea_query::ColumnType;
3
4/// Table primary key definition.
5#[derive(Debug, Clone)]
6pub struct PrimaryKeyDef {
7    pub name: String,
8    pub col_type: ColumnType,
9    pub auto_increment: bool,
10}
11
12impl PrimaryKeyDef {
13    /// Creates a primary key named `name`, defaulting to an auto-incrementing `i32`.
14    pub fn new(name: impl Into<String>) -> Self {
15        Self {
16            name: name.into(),
17            col_type: ColumnType::Integer,
18            auto_increment: true,
19        }
20    }
21
22    /// Uses a 32-bit integer primary key (the default).
23    pub fn i32(mut self) -> Self {
24        self.col_type = ColumnType::Integer;
25        self
26    }
27
28    /// Uses a 64-bit integer primary key.
29    pub fn i64(mut self) -> Self {
30        self.col_type = ColumnType::BigInteger;
31        self
32    }
33
34    /// Uses a UUID primary key. Disables auto-increment, since UUIDs are
35    /// generated client-side rather than by the database sequence.
36    pub fn uuid(mut self) -> Self {
37        self.col_type = ColumnType::Uuid;
38        self.auto_increment = false;
39        self
40    }
41
42    /// Marks the primary key as auto-incrementing.
43    pub fn auto_increment(mut self) -> Self {
44        self.auto_increment = true;
45        self
46    }
47
48    /// Disables auto-increment — the caller is responsible for supplying the
49    /// primary key value on insert.
50    pub fn no_auto_increment(mut self) -> Self {
51        self.auto_increment = false;
52        self
53    }
54}