Skip to content

How to insert nullable (Optional) value ? #1654

Answered by azzamsa
azzamsa asked this question in Q&A
Discussion options

You must be logged in to vote

I find the solution,

@@ -68,11 +68,17 @@ pub async fn create(pool: &PgPool, user_input: CreateUserInput) -> anyhow::Resul
         bail!("a user with same `name` already exists")
     }
 
+    let full_name = if user_input.full_name.is_some() {
+        user_input.full_name
+    } else {
+        None
+    };
+
     let user = sqlx::query_as!(
         User,
         r#"insert into user_ (name, full_name) values ($1, $2) returning *"#,
         &user_input.name,
-        &user_input.full_name,
+        full_name
     )
     .fetch_one(pool)
     .await

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by azzamsa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant