pub struct OnConflict {
pub columns: Vec<String>,
pub action: ConflictAction,
pub where_clause: Option<Vec<LogicCondition>>,
pub update_columns: Option<Vec<String>>,
}Expand description
ON CONFLICT clause for upsert operations.
Supports both basic conflict resolution and advanced features like partial unique indexes and selective column updates.
§Examples
use postgrest_parser::{OnConflict, ConflictAction};
// Basic upsert: ON CONFLICT (email) DO UPDATE SET ...
let conflict = OnConflict::do_update(vec!["email".to_string()]);
assert_eq!(conflict.action, ConflictAction::DoUpdate);
// Ignore conflicts: ON CONFLICT (id) DO NOTHING
let conflict = OnConflict::do_nothing(vec!["id".to_string()]);
assert_eq!(conflict.action, ConflictAction::DoNothing);
// Partial unique index: ON CONFLICT (email) WHERE deleted_at IS NULL
use postgrest_parser::{parse_filter, LogicCondition};
let filter = parse_filter("deleted_at", "is.null").unwrap();
let conflict = OnConflict::do_update(vec!["email".to_string()])
.with_where_clause(vec![LogicCondition::Filter(filter)]);
assert!(conflict.where_clause.is_some());
// Selective update: ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name
let conflict = OnConflict::do_update(vec!["id".to_string()])
.with_update_columns(vec!["name".to_string()]);
assert_eq!(conflict.update_columns.unwrap().len(), 1);Fields§
§columns: Vec<String>Columns that define the conflict target (unique constraint or index)
action: ConflictActionAction to take on conflict: DO NOTHING or DO UPDATE
where_clause: Option<Vec<LogicCondition>>Optional WHERE clause for partial unique index
Example: ON CONFLICT (email) WHERE deleted_at IS NULL
update_columns: Option<Vec<String>>Specific columns to update on conflict (if None, all columns are updated)
Example: ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name
Implementations§
Source§impl OnConflict
impl OnConflict
Sourcepub fn do_nothing(columns: Vec<String>) -> Self
pub fn do_nothing(columns: Vec<String>) -> Self
Creates an ON CONFLICT … DO NOTHING clause.
Sourcepub fn with_where_clause(self, where_clause: Vec<LogicCondition>) -> Self
pub fn with_where_clause(self, where_clause: Vec<LogicCondition>) -> Self
Adds a WHERE clause for partial unique index support.
Sourcepub fn with_update_columns(self, update_columns: Vec<String>) -> Self
pub fn with_update_columns(self, update_columns: Vec<String>) -> Self
Specifies which columns to update (instead of all columns).
Trait Implementations§
Source§impl Clone for OnConflict
impl Clone for OnConflict
Source§fn clone(&self) -> OnConflict
fn clone(&self) -> OnConflict
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for OnConflict
impl Debug for OnConflict
Source§impl<'de> Deserialize<'de> for OnConflict
impl<'de> Deserialize<'de> for OnConflict
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl PartialEq for OnConflict
impl PartialEq for OnConflict
Source§impl Serialize for OnConflict
impl Serialize for OnConflict
impl StructuralPartialEq for OnConflict
Auto Trait Implementations§
impl Freeze for OnConflict
impl RefUnwindSafe for OnConflict
impl Send for OnConflict
impl Sync for OnConflict
impl Unpin for OnConflict
impl UnwindSafe for OnConflict
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more