From ab49dc06be16b84a6e4675e210bea26d0011b482 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Thu, 17 Oct 2024 07:18:47 -0700 Subject: [PATCH] fix --- macro/src/codegen/insert.rs | 8 +++++--- macro/src/codegen/model.rs | 2 +- ormlite/tests/simple.rs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/macro/src/codegen/insert.rs b/macro/src/codegen/insert.rs index d4a9c8a..e5d971b 100644 --- a/macro/src/codegen/insert.rs +++ b/macro/src/codegen/insert.rs @@ -8,13 +8,15 @@ use ormlite_attr::Type; use proc_macro2::TokenStream; use quote::quote; -pub fn impl_Model__insert(db: &dyn OrmliteCodegen, attr: &TableMeta, metadata_cache: &MetadataCache) -> TokenStream { +pub fn impl_Model__insert(db: &dyn OrmliteCodegen, attr: &ModelMeta, metadata_cache: &MetadataCache) -> TokenStream { let box_future = crate::util::box_fut_ts(); let mut placeholder = db.placeholder(); let db = db.database_ts(); let table = &attr.name; let params = attr.database_columns().map(|c| { - if c.has_database_default { + if attr.pkey.name == c.name { + placeholder.next().unwrap() + } else if c.has_database_default { "DEFAULT".to_string() } else { placeholder.next().unwrap() @@ -23,7 +25,7 @@ pub fn impl_Model__insert(db: &dyn OrmliteCodegen, attr: &TableMeta, metadata_ca let query_bindings = attr .database_columns() - .filter(|c| !c.has_database_default) + .filter(|c| attr.pkey.name == c.name || !c.has_database_default) .map(|c| insertion_binding(c)); let insert_join = attr.many_to_one_joins().map(|c| insert_join(c)); diff --git a/macro/src/codegen/model.rs b/macro/src/codegen/model.rs index b6da42c..2876072 100644 --- a/macro/src/codegen/model.rs +++ b/macro/src/codegen/model.rs @@ -11,7 +11,7 @@ pub fn impl_Model(db: &dyn OrmliteCodegen, attr: &ModelMeta, metadata_cache: &Me let model = &attr.ident; let partial_model = attr.builder_struct(); - let impl_Model__insert = impl_Model__insert(db, &attr.table, metadata_cache); + let impl_Model__insert = impl_Model__insert(db, &attr, metadata_cache); let impl_Model__update_all_fields = impl_Model__update_all_fields(db, attr); let impl_Model__delete = impl_Model__delete(db, attr); let impl_Model__fetch_one = impl_Model__fetch_one(db, attr); diff --git a/ormlite/tests/simple.rs b/ormlite/tests/simple.rs index 42341aa..38303c9 100644 --- a/ormlite/tests/simple.rs +++ b/ormlite/tests/simple.rs @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box> { println!("{:?}", john); println!("select"); - let people = Person::select().where_("age > ?").bind(50).fetch_all(&mut conn).await?; + let people = Person::select().where_bind("age > ?", 50).fetch_all(&mut conn).await?; println!("select query builder {:?}", people); let r = sqlx::query_as::<_, Person>("select * from person where age > ?")