Skip to main content

DecoratableTarget

Trait DecoratableTarget 

Source
pub trait DecoratableTarget<P> {
    type FilterOutput;

    // Required method
    fn filter_target(self, predicate: P) -> Self::FilterOutput;
}
Expand description

Adds a WHERE predicate to an ON CONFLICT target.

This enables the ON CONFLICT (target) WHERE predicate DO ... SQL syntax on PostgreSQL. PostgreSQL uses the predicate to select which unique index to match against. Any unique index whose WHERE clause is implied by the predicate qualifies.

Calling .filter_target() multiple times combines the predicates with AND.

Required Associated Types§

Source

type FilterOutput

The type returned by filter_target.

Required Methods§

Source

fn filter_target(self, predicate: P) -> Self::FilterOutput

Adds a WHERE predicate to the ON CONFLICT target, telling PostgreSQL which unique index to check for conflicts (PostgreSQL only).

This generates ON CONFLICT (target) WHERE predicate DO ... SQL. PostgreSQL selects unique indexes whose WHERE clause is implied by the predicate; an exact match is not required.

Calling .filter_target() multiple times combines predicates with AND.

§Example
diesel::insert_into(users)
    .values(name.eq("Sam"))
    .execute(conn)?;

diesel::insert_into(users)
    .values(name.eq("Sam"))
    .on_conflict(name)
    .filter_target(name.like("S%"))
    .do_update()
    .set(name.eq("Updated"))
    .execute(conn)?;

let count = users.filter(name.eq("Updated")).count().get_result::<i64>(conn)?;
assert_eq!(count, 1);

For more examples including predicate chaining, see IncompleteOnConflict’s implementation of this trait.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Stmt, T, P> DecoratableTarget<P> for IncompleteOnConflict<Stmt, T>

Source§

impl<T, P> DecoratableTarget<P> for T
where P: Expression, P::SqlType: BoolOrNullableBool, T: UndecoratedConflictTarget,

Source§

type FilterOutput = DecoratedConflictTarget<T, WhereClause<P>>