geekorm_core::builder::keys

Module primary

Source
Expand description

Primary Key

§Primary Key

Primary Keys are used to uniquely identify a row in a table. GeekORM supports three primary key types:

  • PrimaryKeyInteger (default)
  • PrimaryKeyString
  • PrimaryKeyUuid (requires the uuid feature to be enabled)

§Standard Example

Here is an example of how to use the PrimaryKey.

use geekorm::prelude::*;

#[derive(Table, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Users {
    #[geekorm(primary_key, auto_increment)]
    pub id: PrimaryKeyInteger,
    #[geekorm(unique)]
    pub username: String,
}

let user = Users {
    id: PrimaryKey::from(1),
    username: String::from("JohnDoe")
};

§String Example

Here is an example of how to use the PrimaryKey struct with a String as the primary key.

use geekorm::prelude::*;

#[derive(Table, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Users {
    #[geekorm(primary_key, auto_increment)]
    pub id: PrimaryKeyString,
    #[geekorm(unique)]
    pub username: String,
}

let user = Users {
    id: PrimaryKey::from("1"),
    username: String::from("JohnDoe")
};

§Uuid Example

With the uuid feature enabled, you can use the PrimaryKeyUuid struct to use a Uuid as the primary key.

use geekorm::prelude::*;


#[derive(Table, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Users {
    #[geekorm(primary_key, auto_increment)]
    pub id: PrimaryKeyUuid,
    #[geekorm(unique)]
    pub username: String,
}

let new_uuid = uuid::Uuid::new_v4();
let user = Users {
    id: PrimaryKeyUuid::from(new_uuid),
    username: String::from("JohnDoe")
};

Structs§

Type Aliases§