Skip to content

Commit

Permalink
on_conflict_do_nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jun 6, 2024
1 parent 08bb864 commit 27ce67e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ where
{
TryInsert::from_insert(self)
}

/// On conflict do nothing
pub fn on_conflict_do_nothing(mut self) -> TryInsert<A>
where
A: ActiveModelTrait,
{
let primary_keys = <A::Entity as EntityTrait>::PrimaryKey::iter();
self.query.on_conflict(
OnConflict::columns(primary_keys.clone())
.do_nothing_on(primary_keys)
.to_owned(),
);

TryInsert::from_insert(self)
}
}

impl<A> QueryTrait for Insert<A>
Expand Down Expand Up @@ -319,13 +334,6 @@ where

// helper function for do_nothing in Insert<A>
pub fn from_insert(mut insert: Insert<A>) -> Self {
let primary_keys = <A::Entity as EntityTrait>::PrimaryKey::iter();
insert.query.on_conflict(
OnConflict::columns(primary_keys.clone())
.do_nothing_on(primary_keys)
.to_owned(),
);

Self {
insert_struct: insert,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/empty_insert_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn test(db: &DbConn) {
};

let conflict_insert = Bakery::insert_many([double_seaside_bakery])
.on_empty_do_nothing()
.on_conflict_do_nothing()
.exec(db)
.await;

Expand Down

0 comments on commit 27ce67e

Please sign in to comment.