Repository: SeaQL/sea-orm (rust)

Issue:
Implement `TryGetable` for `Vec<u8>`
There is no implementation of [`TryGetable`](https://docs.rs/sea-orm/0.2.1/sea_orm/trait.TryGetable.html#) for `Vec<u8>`. This should be possible as [sqlx implements it for `Vec<u8>`](https://docs.rs/sqlx/0.5.7/sqlx/trait.Decode.html#impl-Decode%3C%27r%2C%20Postgres%3E-for-Vec%3CT%2C%20Global%3E).

This could be resolved easily by adding `try_getable_all!(Vec<u8>);` to [`src/executir/query.rs`](https://docs.rs/sea-orm/0.2.1/src/sea_orm/executor/query.rs.html#236).

Pull request:
Testing Vec<u8>
Resolve #133 

Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 65adce04..3641e030 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
 log = { version = "^0.4", optional = true }
 rust_decimal = { version = "^1", optional = true }
 sea-orm-macros = { version = "^0.2", path = "sea-orm-macros", optional = true }
-sea-query = { version = "^0.16", features = ["thread-safe"] }
+sea-query = { version = "^0.16.1", features = ["thread-safe"] }
 sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] }
 serde = { version = "^1.0", features = ["derive"] }
 serde_json = { version = "^1", optional = true }
diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs
index fbaaef15..004970c5 100644
--- a/sea-orm-macros/src/derives/entity_model.rs
+++ b/sea-orm-macros/src/derives/entity_model.rs
@@ -167,6 +167,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
                                 "Uuid" => quote! { Uuid },
                                 "Json" => quote! { Json },
                                 "Decimal" => quote! { Decimal },
+                                "Vec<u8>" => quote! { Binary },
                                 _ => {
                                     return Err(Error::new(
                                         field.span(),
diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs
index cfcb0bbd..ea83d97b 100644
--- a/src/entity/active_model.rs
+++ b/src/entity/active_model.rs
@@ -74,14 +74,15 @@ pub trait ActiveModelTrait: Clone + Debug {
         let am = self;
         let exec = <Self::Entity as EntityTrait>::insert(am).exec(db);
         let res = exec.await?;
-        // Assume valid last_insert_id is not equals to Default::default()
-        if res.last_insert_id
-            != <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType::default()
+        // TODO: if the entity does not have auto increment primary key, then last_insert_id is a wrong value
+        // FIXME: Assumed valid last_insert_id is not equals to Default::default()
+        if <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::auto_increment()
+            && res.last_insert_id != <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType::default()
         {
-            let found = <Self::Entity as EntityTrait>::find_by_id(res.last_insert_id)
-                .one(db)
-                .await?;
-            match found {
+            let find = <Self::Entity as EntityTrait>::find_by_id(res.last_insert_id).one(db);
+            let found = find.await;
+            let model: Option<<Self::Entity as EntityTrait>::Model> = found?;
+            match model {
                 Some(model) => Ok(model.into_active_model()),
                 None => Err(DbErr::Exec("Failed to find inserted item".to_owned())),
             }


Test patch:
diff --git a/tests/common/bakery_chain/metadata.rs b/tests/common/bakery_chain/metadata.rs
index b9707059..69579492 100644
--- a/tests/common/bakery_chain/metadata.rs
+++ b/tests/common/bakery_chain/metadata.rs
@@ -8,6 +8,7 @@ pub struct Model {
     pub uuid: Uuid,
     pub key: String,
     pub value: String,
+    pub bytes: Vec<u8>,
 }
 
 #[derive(Copy, Clone, Debug, EnumIter)]
diff --git a/tests/common/setup/schema.rs b/tests/common/setup/schema.rs
index 1a0a1c0a..78947c36 100644
--- a/tests/common/setup/schema.rs
+++ b/tests/common/setup/schema.rs
@@ -281,6 +281,7 @@ pub async fn create_metadata_table(db: &DbConn) -> Result<ExecResult, DbErr> {
         )
         .col(ColumnDef::new(metadata::Column::Key).string().not_null())
         .col(ColumnDef::new(metadata::Column::Value).string().not_null())
+        .col(ColumnDef::new(metadata::Column::Bytes).binary().not_null())
         .to_owned();
 
     create_table(db, &stmt, Metadata).await
diff --git a/tests/primary_key_tests.rs b/tests/primary_key_tests.rs
index ea8255e9..06b2a13a 100644
--- a/tests/primary_key_tests.rs
+++ b/tests/primary_key_tests.rs
@@ -1,7 +1,7 @@
 pub mod common;
 
 pub use common::{bakery_chain::*, setup::*, TestContext};
-use sea_orm::{entity::prelude::*, DatabaseConnection, Set};
+use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
 use uuid::Uuid;
 
 #[sea_orm_macros::test]
@@ -21,18 +21,23 @@ async fn main() -> Result<(), DbErr> {
 }
 
 pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
-    let metadata = metadata::ActiveModel {
-        uuid: Set(Uuid::new_v4()),
-        key: Set("markup".to_owned()),
-        value: Set("1.18".to_owned()),
+    let metadata = metadata::Model {
+        uuid: Uuid::new_v4(),
+        key: "markup".to_owned(),
+        value: "1.18".to_owned(),
+        bytes: vec![1, 2, 3],
     };
 
-    let res = Metadata::insert(metadata.clone()).exec(db).await?;
+    let res = Metadata::insert(metadata.clone().into_active_model())
+        .exec(db)
+        .await?;
+
+    assert_eq!(Metadata::find().one(db).await?, Some(metadata.clone()));
 
     assert_eq!(
         res.last_insert_id,
         if cfg!(feature = "sqlx-postgres") {
-            metadata.uuid.unwrap()
+            metadata.uuid
         } else {
             Default::default()
         }