Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Faq - Bulk upsert with optional fields #2865

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,23 @@ sqlx::query!(
let foo_texts: Vec<String> = vec![/* ... */];
let foo_bools: Vec<bool> = vec![/* ... */];
let foo_ints: Vec<i64> = vec![/* ... */];
let foo_opt_texts: Vec<Option<String>> = vec![/* ... */];
let foo_opt_naive_dts: Vec<Option<NaiveDateTime>> = vec![/* ... */]


sqlx::query!(
"
INSERT INTO foo(text_column, bool_column, int_column)
SELECT * FROM UNNEST($1::text[], $2::bool[], $3::int8[])
INSERT INTO foo(text_column, bool_column, int_column, opt_text_column, opt_naive_dt_column)
SELECT * FROM UNNEST($1::text[], $2::bool[], $3::int8[], $4::text[], $5::timestamp[])
",
&foo_texts[..],
&foo_bools[..],
&foo_ints[..]
&foo_ints[..],
// Due to a limitation in how SQLx typechecks query parameters, `Vec<Option<T>>` is unable to be typechecked.
// This demonstrates the explicit type override syntax, which tells SQLx not to typecheck these parameters.
// See the documentation for `query!()` for more details.
&foo_opt_texts as &[Option<String>],
Vrajs16 marked this conversation as resolved.
Show resolved Hide resolved
&foo_opt_naive_dts as &[Option<NaiveDateTime>]
)
.execute(&db)
.await?;
Expand Down