Skip to content

INSERTS and UPDATES with variable fields #3436

Answered by CommanderStorm
sinder38 asked this question in Q&A
Discussion options

You must be logged in to vote

Okay. More serious try:

Why not a simple COALESCE($1, creator_id)?

use sqlx::PgPool;

async fn print_ingredient(pool:&PgPool){
    let val = sqlx::query!("SELECT * FROM ingredient").fetch_all(pool).await.unwrap();
    println!("ingredients: {val:?}");
}

#[tokio::main]
async fn main() {
    let pool = sqlx::postgres::PgPool::connect("postgres://postgres:CHANGE_ME@localhost:3333/postgres").await.unwrap();
    sqlx::migrate!().run(&pool).await.unwrap();
    let _ = sqlx::query!("INSERT INTO ingredient(creator_id) VALUES (1)").execute(&pool).await;
    print_ingredient(&pool).await;
    
    sqlx::query!(r#"UPDATE ingredient SET
                    creator_id = COALESCE($1, creator_id)

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
10 replies
@sinder38
Comment options

@CommanderStorm
Comment options

Answer selected by sinder38
@sinder38
Comment options

@sinder38
Comment options

@CommanderStorm
Comment options

@sinder38
Comment options

Comment options

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