-
-
Notifications
You must be signed in to change notification settings - Fork 550
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
Comments
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. |
Looking forward to this feature. |
yes this is a very important feature, would love this aswell |
+1 |
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.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 After patching the library, you’ll have access to a new method that can be chained after 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 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 If you're using SQLite, remember to enable the |
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 anInsertMany
struct that has the exec_with_returning function that returns theVec
ofModel
implsCurrent 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.
The text was updated successfully, but these errors were encountered: