-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
Inserting active models by insert_many
with on_conflict
and do_nothing
panics if no rows are inserted.
#899
Comments
Hey @Karahik, welcome! This was the behaviour of PostgreSQL. If you perform the same insert twice with on conflict do nothing and returning. The second insert will do nothing and return nothing. In fact that it return nothing trigger the panic!
I think we should throw an error at the line below instead of panicking: sea-orm/src/executor/insert.rs Line 121 in 1385b74
|
I agree. |
I second this issue, also observed this behavior. |
same problem. It is better if it does not use |
Hey everyone, please check #1021, on PostgreSQL, inserting many records with upsert will throw |
But why we should return an error if record wasn't inserted? I think if someone uses When a new user want to sign up, obviously he needs to create a username, and obviously the username should be unique Usually the code looks like this: let user = User::find()
.filter(users::Column::Username.contains(payload.username))
.one(&db)
.await?;
if user.is_some() {
return ServiceError::Conflit("User already exist");
}
users::ActiveModel {
username: Set(payload.username),
..Default::default()
}
.save(&db)
.await?; It takes two queries, but we can rewrite it with one via let new_user = users::ActiveModel {
username: Set(payload.username),
..Default::default()
};
let user: Option<Model> = Insert::one(new_user)
.on_conflict(OnConflict::column(users::Column::Username).do_nothing())
.exec(&db)
.await?;
if user.is_none() {
return ServiceError::Conflit("Username already exist");
}
Ok(()) I think it's useful functionality. Ofc we can just handle |
I ran into the same problem as well. When I also agree with @Vaider7 in that the expected return should be more of However, I do understand that it may be hard to make such behavioral exemptions to the codebase that is so generically crafted. Thank you for creating a wonderful ORM library, I'm having a blast using it at work and with personal projects. p.s. the PR looks like a clean and simple fix for the issue. |
I'm also hitting this issue, without any insert-many. Simply by inserting an existing row with "do-nothing". |
Does #1208 provide a usable workaround for this issue? |
Hi, I've also been getting this in the lastest release candidate ( |
Yes, please double check your dependency tree |
Description
insert_many
withon_conflit
anddo_nothing
panics if every given key conflicts with existing rows.It seems that the code the error message indicates is trying to retrieve the last inserted row, but it could get nothing because insertion did not happen.
I'd expect this to do nothing (without panics).
Steps to Reproduce
This is what my example project looks like.
Cargo.toml.
This is the example code(main.rs).
Here is the definition of the
fruits
table(./sql/00000000000000_schema.sql).insert_many
in conjunction withon_conflict
anddo_nothing
several times.In the examle above, the
main
function is called twice in the test function.So, you can test this by running
cargo test
.Expected Behavior
When 'do_nothing' is used,
insert_many
ends successfully even if no rows are inserted.Actual Behavior
It panics with the following error.
Reproduces How Often
Always
Versions
The text was updated successfully, but these errors were encountered: