Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example-postgres/migrations/20240908062042_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ CREATE TABLE users
last_login TIMESTAMP DEFAULT NULL
);

CREATE TABLE users_with_string_id
(
id TEXT PRIMARY KEY,
first_name VARCHAR(128) NOT NULL
);

CREATE TABLE test
(
id SERIAL PRIMARY KEY,
Expand Down
13 changes: 12 additions & 1 deletion example-postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ struct UpdateUser {
role: Role,
}

#[derive(Debug, ormx::Table)]
#[ormx(table = "users_with_string_id", id = user_id, insertable, deletable)]
struct UserWithStringId {
// `#[ormx(get_one = ..)]` generates `User::get_by_user_id(db, id: String) -> Result<User>` for us
#[ormx(column = "id", default, get_one = get_by_user_id)] // map this field to the column "id"
user_id: String,

// just some normal, 'NOT NULL' columns
first_name: String,
}

// these are all enums, created using `CREATE TYPE .. AS ENUM (..);`

#[derive(Debug, Copy, Clone, sqlx::Type)]
Expand Down Expand Up @@ -178,4 +189,4 @@ async fn main() -> anyhow::Result<()> {


Ok(())
}
}
3 changes: 2 additions & 1 deletion ormx-macros/src/backend/common/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn impl_table<B: Backend>(table: &Table<B>) -> TokenStream {
impl ormx::Table for #table_ident {
type Id = #id_ty;

fn id(&self) -> Self::Id { self.#id_ident }
fn id(&self) -> Self::Id { self.#id_ident.clone() }

#get
#stream_all
Expand Down Expand Up @@ -83,6 +83,7 @@ fn update<B: Backend>(table: &Table<B>) -> TokenStream {
table.id.column(),
bindings.next().unwrap()
);

let id_argument = &table.id.field;
let other_arguments = table.fields_except_id().map(TableField::fmt_as_argument);

Expand Down
2 changes: 1 addition & 1 deletion ormx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
Self: Sized + Send + Sync + 'static,
{
/// Type of the ID column of this table.
type Id: 'static + Copy + Send;
type Id: 'static + Clone + Send;

/// Returns the id of this row.
fn id(&self) -> Self::Id;
Expand Down