diff --git a/example-postgres/migrations/20250119000000_schema_qualified.sql b/example-postgres/migrations/20250119000000_schema_qualified.sql new file mode 100644 index 0000000..337b9a4 --- /dev/null +++ b/example-postgres/migrations/20250119000000_schema_qualified.sql @@ -0,0 +1,9 @@ +-- Test schema-qualified table names +CREATE SCHEMA IF NOT EXISTS app; + +CREATE TABLE app.products +( + id SERIAL PRIMARY KEY, + name VARCHAR(128) NOT NULL, + price FLOAT8 NOT NULL +); diff --git a/example-postgres/src/main.rs b/example-postgres/src/main.rs index efcd6fd..15ae354 100644 --- a/example-postgres/src/main.rs +++ b/example-postgres/src/main.rs @@ -102,6 +102,17 @@ struct Test { rows: Vec, } +// Example using a schema-qualified table name +#[derive(Debug, ormx::Table)] +#[ormx(table = "app.products", id = id, insertable, deletable)] +struct Product { + #[ormx(default)] + id: i32, + #[ormx(by_ref)] + name: String, + price: f64, +} + #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv::dotenv().ok(); @@ -177,5 +188,21 @@ async fn main() -> anyhow::Result<()> { new.delete(&mut *tx).await?; + info!("test schema-qualified table name (app.products).."); + let product = InsertProduct { + name: "Widget".to_owned(), + price: 19.99, + } + .insert(&mut *tx) + .await?; + info!("inserted product with id {}", product.id); + + let fetched = Product::get(&mut *tx, product.id).await?; + info!("fetched product: {:?}", fetched); + + product.delete(&mut *tx).await?; + info!("deleted product"); + + Ok(()) } diff --git a/ormx-macros/src/table/mod.rs b/ormx-macros/src/table/mod.rs index d8752fc..7b58d13 100644 --- a/ormx-macros/src/table/mod.rs +++ b/ormx-macros/src/table/mod.rs @@ -62,7 +62,13 @@ impl Table { pub fn name(&self) -> String { let q = B::QUOTE; - format!("{q}{}{q}", self.table) + // Handle schema-qualified table names (e.g., "schema.table") + // by quoting each part separately + self.table + .split('.') + .map(|part| format!("{q}{part}{q}")) + .collect::>() + .join(".") } }