-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PostgreSQL insert many with returning
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
pub mod common; | ||
|
||
pub use common::{features::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::{sea_query::OnConflict, Set}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(feature = "sqlx-postgres")] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("upsert_tests").await; | ||
create_tables(&ctx.db).await?; | ||
create_insert_default(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use insert_default::*; | ||
|
||
let on_conflict = OnConflict::column(Column::Id).do_nothing().to_owned(); | ||
|
||
let res = Entity::insert_many([ | ||
ActiveModel { id: Set(1) }, | ||
ActiveModel { id: Set(2) }, | ||
ActiveModel { id: Set(3) }, | ||
]) | ||
.on_conflict(on_conflict.clone()) | ||
.exec(db) | ||
.await; | ||
|
||
assert_eq!(res?.last_insert_id, 3); | ||
|
||
let res = Entity::insert_many([ | ||
ActiveModel { id: Set(1) }, | ||
ActiveModel { id: Set(2) }, | ||
ActiveModel { id: Set(3) }, | ||
ActiveModel { id: Set(4) }, | ||
]) | ||
.on_conflict(on_conflict.clone()) | ||
.exec(db) | ||
.await; | ||
|
||
assert_eq!(res?.last_insert_id, 4); | ||
|
||
let res = Entity::insert_many([ | ||
ActiveModel { id: Set(1) }, | ||
ActiveModel { id: Set(2) }, | ||
ActiveModel { id: Set(3) }, | ||
ActiveModel { id: Set(4) }, | ||
]) | ||
.on_conflict(on_conflict) | ||
.exec(db) | ||
.await; | ||
|
||
assert_eq!( | ||
res.err(), | ||
Some(DbErr::RecordNotInserted( | ||
"None of the records are being inserted".to_owned() | ||
)) | ||
); | ||
|
||
Ok(()) | ||
} |