Skip to content

Commit ca79a48

Browse files
committed
feat: use query_as function instead macro on views to prevent optional
properties mismatch
1 parent a763b5b commit ca79a48

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sqlx_gen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlx-gen"
3-
version = "0.4.4"
3+
version = "0.4.5"
44
edition = "2021"
55
description = "Generate Rust structs from database schema introspection"
66
license = "MIT"

crates/sqlx_gen/src/codegen/crud_gen.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ pub fn generate_crud_from_parsed(
2020
let repo_name = format!("{}Repository", entity.struct_name);
2121
let repo_ident = format_ident!("{}", repo_name);
2222

23-
let table_name = &entity.table_name;
23+
let table_name = match &entity.schema_name {
24+
Some(schema) => format!("{}.{}", schema, entity.table_name),
25+
None => entity.table_name.clone(),
26+
};
2427

2528
// Pool type (used via full path sqlx::PgPool etc., no import needed)
2629
let pool_type = pool_type_tokens(db_kind);
@@ -29,7 +32,7 @@ pub fn generate_crud_from_parsed(
2932
// query_as! macro can't resolve the column type at compile time. Fall back to runtime query_as::<_, T>()
3033
// for queries that return rows. DELETE (no rows returned) can still use macro.
3134
let has_custom_sql_type = entity.fields.iter().any(|f| f.sql_type.is_some());
32-
let use_macro = query_macro && !has_custom_sql_type;
35+
let use_macro = query_macro && !has_custom_sql_type && !entity.is_view;
3336

3437
// Entity import
3538
imports.insert(format!("use {}::{};", entity_module_path, entity.struct_name));
@@ -182,7 +185,7 @@ pub fn generate_crud_from_parsed(
182185
.map(|f| {
183186
let name = format_ident!("{}", f.rust_name);
184187
let ty: TokenStream = f.inner_type.parse().unwrap();
185-
quote! { #name: &#ty }
188+
quote! { #name: #ty }
186189
})
187190
.collect();
188191

@@ -278,7 +281,7 @@ pub fn generate_crud_from_parsed(
278281
&sql_macro,
279282
&binds,
280283
db_kind,
281-
table_name,
284+
&table_name,
282285
&pk_fields,
283286
&non_pk_fields,
284287
use_macro,
@@ -456,7 +459,7 @@ pub fn generate_crud_from_parsed(
456459
.map(|f| {
457460
let name = format_ident!("{}", f.rust_name);
458461
let ty: TokenStream = f.inner_type.parse().unwrap();
459-
quote! { #name: &#ty }
462+
quote! { #name: #ty }
460463
})
461464
.collect();
462465

crates/sqlx_gen_macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlx-gen-macros"
3-
version = "0.4.4"
3+
version = "0.4.5"
44
edition = "2021"
55
description = "No-op attribute macros for sqlx-gen generated code"
66
license = "MIT"

0 commit comments

Comments
 (0)