Skip to content
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

Divide the insert_many logic to an InsertMany struct to support returning from it. #1862

Closed
dvtkrlbs opened this issue Sep 16, 2023 · 6 comments

Comments

@dvtkrlbs
Copy link

Motivation

Currently there does not seem to be any way of inserting many rows and immediately returning it. The use case for this when using database calculated columns such as id columns.

Proposed Solutions

Similar to UpdateMany introduce an InsertMany struct that has the exec_with_returning function that returns the Vec of Model impls

Current Workarounds

Immediately selecting the rows back which might not be possible always. Or the worse but always working workaround is inserting in a loop.

I would be happy to try to implement my proposed solution with some guidance.

@DMaxter
Copy link

DMaxter commented Oct 27, 2023

I am also looking forward for some feature like this. The way I'm currently using it I can only retrieve the latest ID, and would be useful to get all the IDs that were inserted.

@LonelyPale
Copy link

Looking forward to this feature.

@0-don
Copy link

0-don commented Nov 23, 2023

yes this is a very important feature, would love this aswell

@mohamedalichelbi
Copy link

+1

@iivvaannxx
Copy link

I recently encountered a situation where this feature would be highly appreciated. It's strange that you can retrieve a vector of multiple updated models, but not when inserting them. I'm also looking forward to its implementation. In the meantime, I've created a quick and dirty workaround.

I didn’t have the time to dive deeply into the library’s code to implement it in a "clean" and "proper" manner, so I won’t be submitting a pull request. However, if anyone else finds themselves in a similar situation, feel free to use my patch as a functional starting point.

You’ll need to add a patch to your root Cargo.toml file (patches are applied globally) that points to the workaround in my fork:

[patch.crates-io]
sea-orm = { git = "https://github.com/iivvaannxx/sea-orm", branch = "insert-many-returning-vec" }

At the time of writing, this corresponds to version 1.1.0 of sea-orm. I’ll do my best to keep it updated for as long as I’m working on my current project, or until it’s officially implemented.

After patching the library, you’ll have access to a new method that can be chained after insert_many in EntityTrait, like this:

YourEntity::insert_many(vec_to_insert)
  .exec_with_returning_many(db) // instead of exec_with_returning
  .await?;

This will insert all the entries and return a Vec<Model> with the inserted results.

As I mentioned, I implemented this quickly without paying much attention to thorough testing (I only ran one test). It worked for my needs, but I haven't tested it extensively. While I don't expect any issues, it may not work for your specific use case. Additionally, it won't work for MySQL since it doesn't support RETURNING (which you may already be aware of).

If you're using SQLite, remember to enable the sqlite-use-returning-for-3_35 feature in your sea-orm dependency to make it compatible (see this for reference)

@tyt2y3
Copy link
Member

tyt2y3 commented Feb 22, 2025

Thank you all. This has been implemented in d3e7786 and 008f844

@tyt2y3 tyt2y3 closed this as completed Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants