diff --git a/example-postgres/migrations/20240908062042_schema.sql b/example-postgres/migrations/20240908062042_schema.sql index 0b75c74..a4a9e42 100644 --- a/example-postgres/migrations/20240908062042_schema.sql +++ b/example-postgres/migrations/20240908062042_schema.sql @@ -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, diff --git a/example-postgres/src/main.rs b/example-postgres/src/main.rs index efcd6fd..4e37847 100644 --- a/example-postgres/src/main.rs +++ b/example-postgres/src/main.rs @@ -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` 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)] @@ -178,4 +189,4 @@ async fn main() -> anyhow::Result<()> { Ok(()) -} +} \ No newline at end of file diff --git a/ormx-macros/src/backend/common/table.rs b/ormx-macros/src/backend/common/table.rs index e64abac..a057d9c 100644 --- a/ormx-macros/src/backend/common/table.rs +++ b/ormx-macros/src/backend/common/table.rs @@ -21,7 +21,7 @@ pub fn impl_table(table: &Table) -> 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 @@ -83,6 +83,7 @@ fn update(table: &Table) -> 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); diff --git a/ormx/src/lib.rs b/ormx/src/lib.rs index 6cfbca5..6072cb1 100644 --- a/ormx/src/lib.rs +++ b/ormx/src/lib.rs @@ -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;