sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.44k stars 436 forks source link

insert query is returning empty rows [tokio-postgress] #932

Closed ogbanugot closed 2 years ago

ogbanugot commented 2 years ago

Hi, I'm running a simple insert query and would like to get the returning row however, the row returned is empty.

let row = client.query_one(
        "INSERT INTO test (state) VALUES ($1)",
        &[&"user6"],
    ).await?;

    let id: i32 = row.get(0);
    let state: &str = row.get(1);

    println!(
        "found test: {}) {}",
        id, state
    );

When I run the above I'm getting this error Error: Error { kind: RowCount, cause: None } I tried using client.query() initially but the returned rows is empty too. I don't know what I'm missing. Thanks!

sfackler commented 2 years ago

INSERT doesn't return the row. You can ask it to by adding a RETURNING clause if you want: https://www.postgresql.org/docs/current/sql-insert.html

ogbanugot commented 2 years ago

Yes, that works thanks a lot!