-
-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make insert statement not panic when inserting nothing #1708
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a naming thing, may be we can rename InsertAttempt
->TryInsert
, and InsertAttemptResultType
-> TryInsertResult
?
src/query/insert.rs
Outdated
@@ -173,6 +222,7 @@ where | |||
/// }; | |||
/// assert_eq!( | |||
/// cake::Entity::insert(orange) | |||
/// .on_empty_do_nothing() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
umm, why do we need to change the examples here? I think this PR should have no changes to exsiting features
src/query/traits.rs
Outdated
@@ -88,3 +91,95 @@ where | |||
QuerySelect::column_as(self, col, alias) | |||
} | |||
} | |||
|
|||
/// Insert query Trait | |||
pub trait InsertTrait<A>: Sized |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need a new Trait here? it is not strictly necessary? I am cautious about new traits, mostly because of the potential impact to compile time. And introducing a new type to the public API require a lot of thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a must, we just want to share the common methods between Insert
and TryInsert
. Methods such as one
, many
, add
...etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can all implement the same methods. I don't see InsertTrait
being used in a type signature anywhere so I think it is not needed. Unless someone is accepting a generic I: InsertTrait
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is code duplication. Implement the same method twice, for two struct, Insert
and TryInsert
.
We can use macro, but I think InsertTrait
to define shared methods might be better. Both are okay for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the idea is to avoid unused trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. technically TryInsert
contains an Insert
so any implementation can simply be delegated. I am not seeing where's the potential saving in code duplication.
Looks great so far! Please finish up this PR, and then make a follow up PR: So, in https://github.com/SeaQL/sea-orm/blob/master/tests/upsert_tests.rs we have: let res = Entity::insert_many([..])
.on_conflict(on_conflict)
.exec(db)
.await;
assert_eq!(res.err(), Some(DbErr::RecordNotInserted)); Can we add a Then, we'd have (in addition to the above): let res = Entity::insert_many([..])
.on_conflict(on_conflict)
.do_nothing()
.exec(db)
.await;
assert_eq!(res, Ok(TryInsertResult::Conflicted)); I am aware that we'd have to write |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still clean, isn't it
.into_iter() | ||
.filter(|_| false); | ||
|
||
let empty_insert = Bakery::insert_many(empty_iterator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, there is the https://doc.rust-lang.org/std/iter/fn.empty.html
@darkmmon
* Optional Field SeaQL/sea-orm#1513 * .gitignore SeaQL/sea-orm#1334 * migration table custom name SeaQL/sea-orm#1511 * OR condition relation SeaQL/sea-orm#1433 * DerivePartialModel SeaQL/sea-orm#1597 * space for migration file naming SeaQL/sea-orm#1570 * composite key up to 12 SeaQL/sea-orm#1508 * seaography integration SeaQL/sea-orm#1599 * QuerySelect SimpleExpr SeaQL/sea-orm#1702 * sqlErr SeaQL/sea-orm#1707 * migration check SeaQL/sea-orm#1519 * postgres array SeaQL/sea-orm#1565 * param intoString SeaQL/sea-orm#1439 * **skipped** re-export SeaQL/sea-orm#1661 * ping SeaQL/sea-orm#1627 * on empty do nothing SeaQL/sea-orm#1708 * on conflict do nothing SeaQL/sea-orm#1712 * **skipped** upgrade versions * active enum fail safe SeaQL/sea-orm#1374 * relation generation check SeaQL/sea-orm#1435 * entity generation bug SeaQL/sea-schema#105 * **skipped** bug fix that does not require edits * EnumIter change SeaQL/sea-orm#1535 * completed and fixed a previous todo SeaQL/sea-orm#1570 * amended wordings and structures * Edit * Remove temp file --------- Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
🎉 Released In 0.12.1 🎉Thank you everyone for the contribution! |
PR Info
New Features
Bug Fixes
Changes