Function diesel::pg::upsert::on_constraint[][src]

pub fn on_constraint(constraint_name: &str) -> OnConstraint<'_>
Expand description

Used to specify the constraint name for an upsert statement in the form ON CONFLICT ON CONSTRAINT. Note that constraint_name must be the name of a unique constraint, not the name of an index.

Example

use diesel::pg::upsert::*;

conn.execute("ALTER TABLE users ADD CONSTRAINT users_name UNIQUE (name)").unwrap();
let user = User { id: 1, name: "Sean", };
let same_name_different_id = User { id: 2, name: "Sean" };
let same_id_different_name = User { id: 1, name: "Pascal" };

assert_eq!(Ok(1), diesel::insert_into(users).values(&user).execute(&conn));

let inserted_row_count = diesel::insert_into(users)
    .values(&same_name_different_id)
    .on_conflict(on_constraint("users_name"))
    .do_nothing()
    .execute(&conn);
assert_eq!(Ok(0), inserted_row_count);

let pk_conflict_result = diesel::insert_into(users)
    .values(&same_id_different_name)
    .on_conflict(on_constraint("users_name"))
    .do_nothing()
    .execute(&conn);
assert!(pk_conflict_result.is_err());