Skip to main content

upsert

Function upsert 

Source
pub fn upsert<T: Serialize>(
    table: &str,
    key: &str,
    columns: &[&str],
    rows: &[T],
) -> Result<Statement>
Expand description

INSERT ... ON CONFLICT (key) DO UPDATE SET ...: inserts the rows, and updates the listed columns of those whose key already exists (Rails’ upsert_all). key must have a unique index; it is one of columns.

§Errors

As insert, and when key is not one of columns.

§Examples

use ocre::{bulk, serde_json::json};

let rows = [json!({"isrc": "FR1", "plays": 10})];
let upsert = bulk::upsert("tracks", "isrc", &["isrc", "plays"], &rows).unwrap();
assert!(upsert.sql.ends_with("FROM json_each(?1) WHERE true ON CONFLICT (isrc) DO UPDATE SET plays = excluded.plays"));