-
-
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
insert_many failing if the models iterator is empty #873
Comments
...as a workaround, I know I can |
Hey @horacimacias, thanks for catching this "logical error". This has to be checked on run-time. Panicking is definitely not a viable option. Perhaps, throwing a What do you think? |
Thanks. I'm not familiar at all with sea-orm's internals but, isn't there anything that can be checked (e.g. when we're building the sql statement to be sent to db or when we're processing the entity) to see if after consuming/traversing the iterator the entity wasn't added any items, and in this case bypass the operation completely? so, checking this at run-time but doing it in sea-orm and not on the code using it? |
I think insert many without values should be no-op. Or is it because that it might be valid due to |
This is correct. Insert many without value will result in a insert query with default value. i.e. INSERT INTO
"rules"
VALUES
(DEFAULT) RETURNING "id" |
I think no-op (skip insert operation) under the hood and without throwing any error is bad. We should throw an error explicitly. |
sorry, why would a no-op be a bad thing here? Whenever we're looping over an Iterator that happens to be empty, the loop just does nothing. let items : Vec<String> = vec![];
for i in items.iter(){
println!("handling {i}");
} this will print nothing, and it's fine.
Also regarding sorry if I'm missing some fundamentals of how sea-orm works. |
To SeaORM's concern, insert many without a model should be no-op. In SeaQuery, insert many with default but no value could be a valid operation, but that should not affect the semantic here. |
Hi, I've just got bitten by the described empty iterator handling, too.
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "post_facebook_page")]
pub struct Model {
#[sea_orm(primary_key)]
pub post_id: Uuid,
#[sea_orm(primary_key)]
pub facebook_page_id: String,
}
entity::post_facebook_page::Entity::insert_many(facebook_pages_to_add).exec(db).await?;
let facebook_pages_to_add = facebook_pages_to_add.collect::<Vec<_>>();
if not(facebook_pages_to_add.is_empty()) {
entity::post_facebook_page::Entity::insert_many(facebook_pages_to_add).exec(db).await?;
}
Thank you guys, very useful library :) |
@billy1624 @tyt2y3 this issue is still in Triage so asking if any fix to this is decided. |
I am more inclined to make it a no-op, but we need to figure out if there is any edge case. |
🎉 Released In 0.12.1 🎉Thank you everyone for the contribution! |
Description
I'm executing an
insert_many
operations where the iterator is the result of mapping/filtering.Sometimes, as a result of the mapping/filtering, the resulting iterator may be empty.
In this case, the insert_many operation fails; I'd expect this to succeed and the database not being hit at all as there is nothing to do.
I increased the logs and I saw the following:
the entity/struct is the following:
I'm using yugabyte for this which should be using postgres.
Steps to Reproduce
Expected Behavior
I'd expect this to succeed and do nothing on the database.
Actual Behavior
Operation fails and complains about null ìd` which is the primary key in my model.
Reproduces How Often
If the iterator is empty, always.
Versions
Additional Information
The text was updated successfully, but these errors were encountered: